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

javascript - SyntaxError: Unexpected token if - Stack Overflow

programmeradmin1浏览0评论

I'm currently learning javascript and I keep having this error!!!

This is my script:

var pare = function(choice1, choice2) 
    if (choice1 === choice2) {
        return "The result is a tie!";
    }
    else if (choice1 === "rock")   
        if (choice2 === "scissors") {
            return "rock wins"; 
        }   
        else {
            return "paper wins";
        }

I'm currently learning javascript and I keep having this error!!!

This is my script:

var pare = function(choice1, choice2) 
    if (choice1 === choice2) {
        return "The result is a tie!";
    }
    else if (choice1 === "rock")   
        if (choice2 === "scissors") {
            return "rock wins"; 
        }   
        else {
            return "paper wins";
        }
Share Improve this question edited Jun 18, 2015 at 16:27 lakshayg 2,1732 gold badges22 silver badges36 bronze badges asked Jun 18, 2015 at 15:51 Brandon MagroBrandon Magro 331 gold badge1 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

Should be:

var pare = function(choice1, choice2){

    if (choice1 === choice2) { return "The result is a tie!"; }
    else if (choice1 === "rock")
        if (choice2 === "scissors") { return "rock wins"; }
    else
        return "paper wins";
}

Or neater:

var pare = function(choice1, choice2){

    if(choice1 === choice2){
        return "The result is a tie!"
    }else if(choice1 === "rock"){
        if(choice2 === "scissors") {
            return "rock wins"
        }
    }else{
        return "paper wins"
    }
}
发布评论

评论列表(0)

  1. 暂无评论