Does Javascript Have A Built In Range() Method? - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › Does Javascript Have A Built In Range() Method? (1222 Views)
| Does Javascript Have A Built In Range() Method? by spartan117(op): 5:24pm On Jun 14, 2020 |
Good day programmers, I tried to get the range of a set of numbers and console.log the sum. Sounds simple right? I tried using console.log(range(1,200)); And console.log(Range (1,200); But I kept getting a reference error: range or Range is not defined. Then in frustration I tried doing Math.range(1,200) hoping that JavaScript might have a built in range method on it's Math object ![]() Finally I resigned myself to printing the numbers in that range with a for loop and pushing the result to an empty array with the .push method on arrays. I want to know how I could have done this using the range method. Thanks. |
| Re: Does Javascript Have A Built In Range() Method? by peterincredible: 11:01pm On Jun 14, 2020 |
spartan117:bros in javascript there is nothing like "range()" function ooo but in python their is a range() function |
| Re: Does Javascript Have A Built In Range() Method? by spartan117(op): 11:14pm On Jun 14, 2020 |
peterincredible:Thought as much |
| Re: Does Javascript Have A Built In Range() Method? by tensazangetsu20(m): 11:31pm On Jun 14, 2020 |
Nope. |
| Re: Does Javascript Have A Built In Range() Method? by Maskyy(m): 9:20am On Jun 15, 2020 |
range = (start, end) => { return Array.from({ length: end - start + 1 }, (_, i) => i) } range(3,50) The above is what I can suggest. Which will produce 3 to 50 |
| Re: Does Javascript Have A Built In Range() Method? by spartan117(op): 6:09pm On Jun 15, 2020 |
Maskyy:Thanks for this code. I find it strange that JavaScript doesn't have a built in method for this. |
| Re: Does Javascript Have A Built In Range() Method? by Maskyy(m): 7:24pm On Jun 15, 2020 |
spartan117:Not actually strange. Every language has a different building community. If it pleased js, they can do a workaround and add the above code as path of their Math method. But I can still save this code and call the function anytime. More like my own script or library. |
| Re: Does Javascript Have A Built In Range() Method? by spartan117(op): 9:31am On Jun 16, 2020 |
Maskyy:I guess that's true. |
| Re: Does Javascript Have A Built In Range() Method? by spartan117(op): 10:36am On Jun 16, 2020 |
Maskyy:I guess you are right. I'll start adding code I feel I may need again down the line to a personal library. |
| Re: Does Javascript Have A Built In Range() Method? by von02: 1:51pm On Feb 04, 2021 |
//Using for loop
function range(min,max){
var result=Number();
for(;min<max+1;min++){
result += min
}
return result;
}
console.log(range(2,10)) |
| Re: Does Javascript Have A Built In Range() Method? by shegun4sur: 12:50pm On Apr 19, 2021 |
Maskyy:Please what is the relationship between what you wrote above and this console.log(range(1,200)) |
| Re: Does Javascript Have A Built In Range() Method? by richebony: 10:02pm On Apr 19, 2021 |
Maskyy:I think this should be the correct function const range = ( start, stop) => Array.from( { length: ( stop - start ) + 1 }, ( _, i ) => start + i ); console.log(range(3,50)); |
Can An App Like Whatsapp Be Built In Nigeria And How Much Will It Cost • Mobile Technology: Lets Build A Mobile App With Javascript • Free HTML5, CSS3 And Basic Javascript (jquery) Training • 2 • 3 • 4
Advice A Newbie Venturing Into Tech • Pirates Of Silicon Valley • How Can I Accept Credit Card On My Website
