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; }
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();
+ 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.
0
class Dog {
constructor(name) {
.name = name;
}
bark() {
console.log(this.
+ ' barks.');
}
}
let d = new Dog('Rex');
d.
();
0
Ashish Kumar: You need this.name and d.bark()
0
this
name
bark
- 1
mga boboo