Last active
April 24, 2018 17:55
-
-
Save multiparadigma/0b8f1328d5458ab8cf104a8141fdee59 to your computer and use it in GitHub Desktop.
rangeFunction.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sum(array=[]){ | |
// Todo | |
// Only if numeric array | |
console.log("typeof: " + typeof array); | |
var sum = 0; | |
for(var index=0; index < array.length; index++){ | |
sum += array[index]; | |
} | |
return sum; | |
} | |
// So what is the arguments object? | |
// Basically, an Array-like object corresponding to the arguments passed to a function. | |
// I thought that using arguments would be the best as a solution to a function using default values | |
// So then, given the numbers of arguments passed into the range() function, | |
// The if-statement will reorganize them all to produce the same effect that the range() function does in Python | |
// Am I being clear? | |
// Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments | |
// After finishing this challenge, without cheating, I did some searches online to check how others are dealing with. | |
// What I found interesting is that, basically, you are free to write weird stuff in JavaScript | |
// Well, at least for me... But somehow what I call "weird" usually reduces a lot of typing. | |
// For example, the Ternary operator is kind of cool. It helps a lot, and many others used to code the range() function | |
// But still, I don't know if those answer are too simple or if I'm just looking for complexity | |
// I tend to write a lot of code sometimes, and most of them are repetitive | |
// Anyway, but even the other functions using ternary operator couldn't handle a function call like the following: | |
// range(10) would return an empty array, but I want it to return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
// Parameters without defaults after default parameters | |
// I don't know if this is some kind of convention, but usually parameters without defaults value are set left-to-right | |
// The first time I tried I probably did something wrong, | |
// I still think I can make the function work reordering the parameters as (start=0, stop, step=[1]) | |
function range(stop, start=0, step=1){ | |
console.log("Range(start=" + start + ", stop=" + stop + ", step=" + step + ")"); | |
var index; | |
for(index = 0; index < arguments.length; index++){ | |
if(typeof arguments[index] == 'number'){ | |
if(arguments.length == 1){ | |
stop = arguments[0]; | |
} else if (arguments.length == 2){ | |
start = arguments[0]; | |
stop = arguments[1]; | |
} else if (arguments.length == 3){ | |
start = arguments[0]; | |
stop = arguments[1]; | |
step = arguments[2]; | |
} | |
} else { | |
if(arguments.length > 1){ | |
console.log("Invalid argument given: " + arguments[index]); | |
} else { | |
console.log("Invalids arguments given: " + arguments[index]); | |
} | |
} | |
console.log("start= " + start + ", stop= " + stop + ", step= " + step); | |
console.log("..."); | |
} | |
var rangeArray = []; | |
while(start <= stop){ | |
rangeArray.push(start); | |
start += step; | |
} | |
console.log("return:"); | |
return rangeArray; | |
} | |
console.log(range()); | |
console.log(range('a')); | |
console.log(10, 'a'); | |
console.log(range(10)); | |
console.log(range(1, 10)); | |
console.log(range(1, 10, 2)); | |
console.log(sum(range(1, 10))); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment