I want to work with the ternary-operator but I get the following error message:
" Unexpected token, expected : "
Why is that?
This is my first code:
const GetUp = (num) => {
for (let i = 1; i <= num; i++) {
if (i % 3 === 0) {
console.log('Get')
}
if (i % 5 === 0) {
console.log('Up')
}
if (i % 3 === 0 && i % 5 === 0) {
console.log('GetUp')
} else {
console.log(i)
}
}
}
GetUp(200)
This is my recent code:
const SetRuc = (num) => {
for (let i = 1; i <= num; i++) {
(i % 3 === 0) ? console.log('Set')
(i % 5 === 0) ? console.log('Ruc')
(i % 3 === 0 && i % 5 === 0) ? console.log('SetRuc') : console.log(i)
}
}
SetRuc(100)
use && for shothand if without else
add semicolumns ; to let it know that it's the end of the instruction, otherwise it will evaluate the three lines as one instruction.
const SetRuc = (num) => {
for (let i = 1; i <= num; i++) {
(i % 3 === 0) && console.log('Set');
(i % 5 === 0) && console.log('Ruc');
(i % 3 === 0 && i % 5 === 0) ? console.log('SetRuc') : console.log(i);
}
}
SetRuc(100)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With