0

I need to find to amount of odd numbers from 0 to num

It's in Javascript and I can't change to first line https://code.sololearn.com/WFRc1DISfp0J/?ref=app

27th Jul 2022, 7:59 AM
k gready
k gready - avatar
15 ответов
+ 8
k gready I am still not sure of how you are intending to resolve your concept, but I would do it this way. var num = 10; var odd_sum = 0; for(i=0;i<=num;i++){ if(i%2==1){ odd_sum += i; } } console.log(odd_sum)
27th Jul 2022, 11:07 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
You have some missing and misplaced {}, you also not returning value from functions. You reset value num, what you should get pased inside function, to be 1, and than you do something, with wrong data. Also why having nested function when you need only 1 to solve this. This code is too complicated to fix and debug it is much easier to code from skretch.
27th Jul 2022, 10:28 AM
PanicS
PanicS - avatar
+ 3
Check comments, to understand where you make mistake, and how can you fix it https://code.sololearn.com/WpZ35dXAaKkq/?ref=app
27th Jul 2022, 1:08 PM
PanicS
PanicS - avatar
+ 3
Assuming <n> was supposedly a positive value, and knowing that 1 is the nearest odd number from 0, I think you should 1. Start your loop counter <i> from 1 2. Repeat loop while <i> is less than or equal to <n>, and increment <i> by 2 on each iteration. This is done in loop construct. 3. Inside loop body, increment <odd_num> by 1 in each iteration. Good luck! 👍
27th Jul 2022, 1:14 PM
Ipang
+ 2
k gready Why do you need to have that complicated const? I am a firm believer in simplicity, unless I am having a joke with someone
27th Jul 2022, 10:16 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
I suggest you to go back and learn loops and how function works.Build fundamental understanding
28th Jul 2022, 7:00 AM
Mr Barrel
Mr Barrel - avatar
+ 1
Maybe I missed it but did you declare the variable "n"? Also, in line 8 (I think) you have ÷ plus =.
27th Jul 2022, 8:07 AM
Ausgrindtube
Ausgrindtube - avatar
+ 1
k gready I can't understand your code because it is either too clever for me, or all over the shop, but I believe your placement of {} is contributing to your problem.
27th Jul 2022, 9:58 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Let me ask you this-how would you write a program that returns the amount of the odd numbers from 0 to num? With using the variable-const countOddFromZeroToNum=(num)=>{
27th Jul 2022, 10:03 AM
k gready
k gready - avatar
+ 1
Thank you all!
27th Jul 2022, 1:17 PM
k gready
k gready - avatar
+ 1
You can try this out, it's wrapped in a function the way you want it. function oddCounter(num){ const countOddFromZeroToNum = num; let x = 0; let count = 0; if(!num){ alert("num can't be empty") } while(x<=countOddFromZeroToNum){ if(x%2===1){ count++ } x++ } console.log(count) } oddCounter(num=3)
28th Jul 2022, 10:01 PM
Bamisile Tolulope
Bamisile Tolulope - avatar
0
It's in 2 line
27th Jul 2022, 8:09 AM
k gready
k gready - avatar
0
https://code.sololearn.com/W1hTDhwugMkm/?ref=app I did it like this and it is still doesn't returns the amount of the odd numbers from 0 to num (inclusive).
27th Jul 2022, 10:37 AM
k gready
k gready - avatar
0
I think 01
28th Jul 2022, 12:46 PM
Nald Rodriguez
Nald Rodriguez - avatar
0
there are some bugs in the code check this out to see as i fixed it https://code.sololearn.com/WfNiD5ZAPBnm/#
2nd Aug 2022, 12:54 AM
Bamisile Tolulope
Bamisile Tolulope - avatar