最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unexpected token, expected : - Stack Overflow

programmeradmin1浏览0评论

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)

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)
Share Improve this question edited Apr 10, 2018 at 15:26 Iván asked Apr 10, 2018 at 15:20 IvánIván 4412 gold badges6 silver badges20 bronze badges 6
  • 1 When you use the ternary operator you have to give it an alternative (after a :) like you did in the last one. That's why it's called "ternary": because it has three parts, that is condition ? true-branch : false-branch – Federico klez Culloca Commented Apr 10, 2018 at 15:21
  • 2 And your case is not a really good use of ternary operators, stick to if – Jean Rostan Commented Apr 10, 2018 at 15:22
  • 1 looks like Fizz Buzz under a different name. – phuzi Commented Apr 10, 2018 at 15:24
  • It does look like Fizz Buzz, but at multiples of 5 and 3 this logs out "Set Ruc SetRuc" instead of just "SetRuc" – Luca Kiebel Commented Apr 10, 2018 at 15:27
  • 1 About your original code (the new one has the same problem), supposing it has to follow the classic rules for fizzbuzz, put the last condition first and use else if after the first one. – Federico klez Culloca Commented Apr 10, 2018 at 15:38
 |  Show 1 more ment

5 Answers 5

Reset to default 6

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)

EG this:

(i % 3 === 0) ? console.log('Set')

provides no : option for the ?. If you don't want anything to happen in the event that the ? check is false, you can simply provide an empty object, or undefined:

(i % 3 === 0) ? console.log('Set') : {}

If you don't want to do anything in case of false result in ternary operator. you can just say something like statement ? 'expression' : null

just mention null in there. Something like

const SetRuc = (num) => {
  for (let i = 1; i <= num; i++) { 
    (i % 3 === 0) ? console.log('Set') : null;
    (i % 5 === 0) ? console.log('Ruc') : null;
    (i % 3 === 0 && i % 5 === 0) ? console.log('SetRuc') : console.log(i);
  }
}

SetRuc(100)

You misuse the ternary operator, the syntax is:

condition ? expr1 : expr1

Assuming that expr1 will be executed if the condition is true, otherwise expr2 will.

So you may want this:

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)
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)

you have missed : after console.log('Set') and console.log('Ruc')

发布评论

评论列表(0)

  1. 暂无评论