Stats: 3,191,773 members, 7,945,409 topics. Date: Tuesday, 10 September 2024 at 04:32 PM |
Nairaland Forum / Science/Technology / Programming / Does Javascript Have A Built In Range() Method? (1000 Views)
Can An App Like Whatsapp Be Built In Nigeria And How Much Will It Cost / Challenge For Javascript Beginners/intermidiate / After 10days Of Coding With Html, Css And Javascript Forum4africa Is Ready (2) (3) (4)
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 ![]() 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(m): 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(m): 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(m): 9:31am On Jun 16, 2020 |
Maskyy: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: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, 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)); |
(1) (Reply)
Separate Yourself From The Other Programmers, Become A Cyber Secure Coder / Can Anyone Help Me To Get Started With Vb.net / Sms Short Code Help
(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 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 45 |