Javascript没有返回结果 [英] Javascript not returning results

查看:107
本文介绍了Javascript没有返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好 -  


有人可以告诉我为什么我的所有"console.log"都没有返回任何内容吗?


我所看到的只是"计算机:"选择" " 






var userChoice = prompt("你选择摇滚,纸张还是剪刀?");
$
var computerChoice = Math.random();

if(computerChoice< 0.34){

computerChoice =" rock"; $
} else if(computerChoice< = 0.67){

computerChoice =" paper"; $
} else {

computerChoice =" scissors" ;; $
} console.log(" Computer:" ; + computerChoice);
$


var compare = function(choice1,choice2){

  &NBSP; if(choice1 === choice2){

  &NBSP; &NBSP; &NBSP; console.log("结果是平局!");

  &NBSP; &NBSP;   

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; } else if(choice1 ===" rock"){

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(choice2 ===" scissors"){

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; console.log("Rock wins");

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }其他{

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; console.log("Paper wins"); 

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; } 

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }   

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   else if(choice1 ===" paper"){

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(choice2 ==="rock"){

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; console.log("Paper wins");

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; } else { 

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; console.log("Scissors wins");

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;否则如果(choice1 ==="剪刀"){

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(choice2 ==="paper"){

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; console.log("Scissors wins");

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }其他{

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; console.log("Rock wins");

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }
console.log(compare(userChoice,computerChoice));

};

解决方案

嗨。行 console.log(compare(userChoice,computerChoice);; )位于compare函数本身内部,因此永远不会被调用。如果您将该行移出,如下所示,它将被调用。此函数也没有返回任何内容,只是
写入控制台本身,所以你只需要调用compare。

 var userChoice = prompt("你选择摇滚,纸张还是剪刀?"); 

var computerChoice = Math.random();
if(computerChoice< 0.34){
computerChoice =" rock" ;;
}否则if(computerChoice< = 0.67){
computerChoice =" paper" ;;
} else {
computerChoice =" scissors" ;;
}

var compare = function(choice1,choice2){
if(choice1 === choice2){
console.log("结果是a领带"!);

}否则如果(choice1 ===" rock"){
if(choice2 ===" scissors"){
console.log(" Rock wins") );
} else {
console.log(" Paper wins");
}
}否则如果(choice1 ===" paper"){
if(choice2 ===" rock"){
console.log(" Paper)胜");
} else {
console.log(&#Scissors wins");
}
}否则if(choice1 ===" scissors"){
if(choice2 ===" paper"){
console.log(" Scissors)胜");
} else {
console.log(" Rock wins");
}
}
};

console.log(" Computer:" + computerChoice);

比较(userChoice,computerChoice);



Hello- 

Can someone tell me why all my "console.log"'s are not returning anything?

All I see is "computer: "choice" " 

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        console.log("The result is a tie!");
        
            } else if (choice1 === "rock") {
                if (choice2 === "scissors") {
                    console.log("Rock wins");
                        } else {
                    console.log("Paper wins"); 
                    } 
                }    
             else if (choice1 === "paper") {
                if (choice2 === "rock") {
                    console.log("Paper wins");
                        } else { 
                    console.log("Scissors wins");
                    }
                }
              else if (choice1 === "scissors") {
                  if (choice2 === "paper") {
                    console.log("Scissors wins");
                        } else {
                    console.log("Rock wins");
                    }
                }
console.log(compare(userChoice, computerChoice));
};

解决方案

Hi. The line console.log(compare(userChoice, computerChoice));is inside the compare function itself so it is never getting called. If you move that line out, as below, it will get called. Also the function isn't returning anything but is writing to the console itself so you just need to call compare.

var userChoice = prompt("Do you choose rock, paper or scissors?");

var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if (computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

var compare = function (choice1, choice2) {
    if (choice1 === choice2) {
        console.log("The result is a tie!");

    } else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            console.log("Rock wins");
        } else {
            console.log("Paper wins");
        }
    } else if (choice1 === "paper") {
        if (choice2 === "rock") {
            console.log("Paper wins");
        } else {
            console.log("Scissors wins");
        }
    } else if (choice1 === "scissors") {
        if (choice2 === "paper") {
            console.log("Scissors wins");
        } else {
            console.log("Rock wins");
        }
    }
};

console.log("Computer: " + computerChoice);

compare(userChoice, computerChoice);


这篇关于Javascript没有返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆