Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected token, expected :

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)
like image 521
Iván Avatar asked Sep 30 '26 09:09

Iván


1 Answers

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)
like image 173
Taki Avatar answered Oct 02 '26 00:10

Taki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!