what will be the outcome for this function ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 2

what will be the outcome for this function ?

function sum_odd(){ var sum = 0; for( var i = 5; i < 174; i++){ if(i % 2==1){ sum+=i; } }

11th Feb 2020, 6:08 PM
Tsering Kalden
Tsering Kalden - avatar
33 Answers
+ 3
your function will sum all the numbers in the range from 5 to 173, which when divided by 2 will show the remainder of division 1
11th Feb 2020, 6:32 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 3
The code will output the sum of odd numbers from 5 to 173
13th Feb 2020, 1:19 AM
Tolib
+ 2
Hello Tsering Kalden You can put that snippet in a JS pane of a new Web code to find out the value of sum. Simply add `console.log(sum);` after the loop body, or you can just use an alert box to show value of <sum> variable.
11th Feb 2020, 6:30 PM
Ipang
+ 2
Do I need console.log(sum) at the end.
11th Feb 2020, 6:35 PM
Tsering Kalden
Tsering Kalden - avatar
+ 1
I try input console.log in different variables, but nothing exit to the window. 🤔 And your code missing one "}"
11th Feb 2020, 6:56 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
create another variable and assign it the value of the entire function. inside the function, add another line of code with the output of the sum variable. use the last line of code to display this new variable. did I make it clear?
11th Feb 2020, 7:09 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
Hi @lpang. i tried and it was nothing happen.
11th Feb 2020, 8:00 PM
Tsering Kalden
Tsering Kalden - avatar
+ 1
Tsering Kalden Can you share your code link here so I can see what's the problem? * How to share links: https://www.sololearn.com/post/74857/?ref=app
11th Feb 2020, 11:25 PM
Ipang
+ 1
He want to output sum. but I doubt that he wrote the code himself (judging by the two previous questions)
12th Feb 2020, 12:02 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
How input code here: If you want to run a code on Sololearn then, ➡️ Go to the Code Playground section (you can find it here : https://code.sololearn.com) and click on the + icon at the bottom right corner. ➡️ Then select Web and write your JS code in the JS section and save it. ➡️ Then click on Run and your Code will get executed.
12th Feb 2020, 12:44 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
Tsering Kalden if you want to tag a user in a message, type @ and wait a bit (a pop - up window with names will appear soon)
12th Feb 2020, 12:47 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
I managed to bring sum to the screen in a calm environment and with minimal code modification. if you haven't figured it out yet, ask me how 🤭 😉
12th Feb 2020, 12:57 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
Console it bro, nobody gonna console it for you
13th Feb 2020, 2:18 AM
Prabesh gauli
Prabesh gauli - avatar
+ 1
you need to do console.log(“Sum”, sum)
13th Feb 2020, 8:41 AM
Sergey
Sergey - avatar
+ 1
I try
13th Feb 2020, 8:50 AM
Shoaib Farooq
Shoaib Farooq - avatar
+ 1
1
13th Feb 2020, 9:07 AM
Divyosmi Goswami
Divyosmi Goswami - avatar
+ 1
In range from 5 to 173
13th Feb 2020, 4:54 PM
Patrick Kivungila
0
Just so you can see the value of variable <sum> Tsering Kalden
11th Feb 2020, 6:54 PM
Ipang
0
I had same issue. I tried different ways but nothing Ярослав Вернигора(Yaroslav Vernigora)
11th Feb 2020, 6:59 PM
Tsering Kalden
Tsering Kalden - avatar
0
Ok let me try with that Ipang
11th Feb 2020, 7:00 PM
Tsering Kalden
Tsering Kalden - avatar