Welcome, Guest: Join Nairaland / LOGIN! / Trending / Recent / New
Stats: 2,755,533 members, 6,546,553 topics. Date: Tuesday, 19 October 2021 at 11:28 PM

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

Nairaland Forum / Science/Technology / Programming / Does Javascript Have A Built In Range() Method? (697 Views)

How Did You Learn Javascript? / Can An App Like Whatsapp Be Built In Nigeria And How Much Will It Cost / Lets Learn React Native,javascript,react Js And Node Js Together(alimosho LGA) (2) (3) (4)

(1) (Reply) (Go Down)

Does Javascript Have A Built In Range() Method? by spartan117(m): 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(m): 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(m): 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(m): 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.

1 Like

Re: Does Javascript Have A Built In Range() Method? by spartan117(m): 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.

1 Like

Re: Does Javascript Have A Built In Range() Method? by von02: 1:51pm On Feb 04
//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
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
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)

A Friend Got Scammed Trying To Learn Programming ? / Free SMS System To Monitor Drug Efficacy & Hiv Positive Daily Medication Confir / Help With A Php Or Dreanweaver

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2021 Oluwaseun Osewa. All rights reserved. See How To Advertise. 63
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.