-
-
Save evanhutomo/c20fd03ea7c74deda1f4 to your computer and use it in GitHub Desktop.
ELOQUENT JAVASCRIPT exercise - The sum of a range
This file contains hidden or 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 () { | |
/* | |
console.log(sum(range(1, 10))); | |
-> 55 | |
console.log(range(5, 2, -1)); | |
-> [5,4,3,2] | |
*/ | |
function range (param1, param2, param3) { | |
/* | |
1. if param1 >= param2 then -param3 else return false | |
2. if param1 <= param2 then +param3 else return false | |
*/ | |
var arrTemp = []; | |
//check number of an argument | |
if (typeof param1 == 'number' && | |
typeof param2 == 'number' && | |
typeof param2 == 'number') { | |
if (param1 >= param2) { | |
if ((arguments.length == 3) && (param3 < 0)) { | |
for (param1; param1 >= param2; param1 -= Math.abs(param3)) { | |
arrTemp.push(param1); | |
} | |
} else if (arguments.length == 2) { | |
for (param1; param1 >= param2; param1--) { | |
arrTemp.push(param1); | |
} | |
} | |
return arrTemp; | |
} else if (param1 <= param2) { | |
if ((arguments.length == 3) && (param3 > 0)) { | |
for (param1; param1 <= param2; param1 += Math.abs(param3)) { | |
arrTemp.push(param1); | |
} | |
} else if (arguments.length == 2) { | |
for (param1; param1 <= param2; param1++) { | |
arrTemp.push(param1); | |
} | |
} | |
return arrTemp; | |
} | |
} | |
} | |
// lets test it! | |
console.log(range(1, 20)); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment