₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,089 members, 8,420,257 topics. Date: Thursday, 04 June 2026 at 02:54 PM

Toggle theme

Does Javascript Have A Built In Range() Method? - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingDoes Javascript Have A Built In Range() Method? (1222 Views)

1 Reply (Go Down)

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 cheesy

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:
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 cheesy

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.
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:
bros in javascript there is nothing like "range()" function ooo but in python their is a range() function
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:
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
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:
Thanks for this code.

I find it strange that JavaScript doesn't have a built in method for this.
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:
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.
I guess that's true.
Re: Does Javascript Have A Built In Range() Method? by spartan117(op): 10:36am On Jun 16, 2020
Maskyy:
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.
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:
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
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:
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
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));
1 Reply

Can An App Like Whatsapp Be Built In Nigeria And How Much Will It CostMobile Technology: Lets Build A Mobile App With JavascriptFree HTML5, CSS3 And Basic Javascript (jquery) Training234

Advice A Newbie Venturing Into TechPirates Of Silicon ValleyHow Can I Accept Credit Card On My Website