admin管理员组文章数量:1356515
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 iscondition ? 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
5 Answers
Reset to default 6use &&
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')
本文标签: javascriptunexpected tokenexpected Stack Overflow
版权声明:本文标题:javascript - Unexpected token, expected : - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744016583a2576440.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论