0

I need help with it please

Starting with the basic function given below, write a function called sumOddNumbers that will print to the console and return the sum of all the odd numbers from 1 to 5000. Consider using a loop and dont forget to call the function afterwards! Function sumOddNumbers() { Var sum = 0; //Your code here Console.log(sum); Return sum; }

2nd Apr 2018, 4:20 AM
Coder1995
Coder1995 - avatar
6 Answers
+ 3
function sumOddNumbers() { let sum = 0; for(let i = 1; i < 5000; i+=2) { sum += i; } console.log(sum); return sum; } let s = sumOddNumbers();
2nd Apr 2018, 4:40 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
You just need to enter 5000 in the text box, and click the button: https://code.sololearn.com/W57G0nYpjWKq/#js With some nice maths from here: https://socratic.org/questions/how-do-you-add-all-the-odd-numbers-between-1-99-inclusive Mine is of complexity O(n) rather than O(n^2), which makes it more efficient than looping through all the possible values.
2nd Apr 2018, 5:04 AM
Emma
0
class Dog { constructor(name) { .name = name; } bark() { console.log(this. + ' barks.'); } } let d = new Dog('Rex'); d. ();
13th Sep 2019, 5:14 AM
Ashish Kumar
Ashish Kumar - avatar
0
Ashish Kumar: You need this.name and d.bark()
18th Nov 2019, 1:02 AM
Libor Benes
Libor Benes - avatar
0
this name bark
20th Sep 2022, 11:46 AM
LIPSA BEHERA
- 1
mga boboo
10th Nov 2019, 5:24 AM
Justine Angelo Bracero
Justine Angelo Bracero - avatar