Reputation: 9
I am trying to solve this loop but I can't solve it
You must create a function called loopDePares that receives a number as a parameter and loops from 0 to 100, displaying each loop number on the console. If the number of the iteration, added to the number passed by parameter, is even, it will show in the console “The number x is even”.
what i did was this
function loopDePartes(numero){
for (let i = 0; i <numero; i++){
console.log(i+numero % 2 ? i : "x")
}
}
loopDePartes(10)
Upvotes: 0
Views: 62
Reputation: 21475
You had one math error, and one comparison error.
i+numero
needs to be wrapped in parens so it take precedence over the modulus operator.
Your ternary was checking the truthiness of the output of that math, instead of checking whether it was even or odd.
Both are corrected below:
function loopDePartes(numero){
for (let i = 0; i < 100; i++){
console.log((i+numero) % 2 == 1
? i
: `The number ${i+numero} is even`
)
}
}
loopDePartes(10)
(Actually, the more I reread your question the less I understand what is the expected output; I think this is what you're looking for but I may have misinterpreted what you meant by "display the loop number", and what "x" is supposed to be in "The number x is even".)
Upvotes: 1