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
15 Answers
+ 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)
+ 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.
+ 3
Check comments, to understand where you make mistake, and how can you fix it
https://code.sololearn.com/WpZ35dXAaKkq/?ref=app
+ 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! 👍
+ 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
+ 2
I suggest you to go back and learn
loops and how function works.Build fundamental understanding
+ 1
Maybe I missed it but did you declare the variable "n"?
Also, in line 8 (I think) you have ÷ plus =.
+ 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.
+ 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)=>{
+ 1
Thank you all!
+ 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)
0
It's in 2 line
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).
0
I think 01
0
there are some bugs in the code
check this out to see as i fixed it
https://code.sololearn.com/WfNiD5ZAPBnm/#