本文介绍了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){ if(choice1 === choice2){ console.log("结果是平局!"); } else if(choice1 ===" rock"){ if(choice2 ===" scissors"){ console.log("Rock wins"); }其他{ console.log("Paper wins"); } } else if(choice1 ===" paper"){ if(choice2 ==="rock"){ console.log("Paper wins"); } else { console.log("Scissors wins"); }¥b $ b }¥b $ b 否则如果(choice1 ==="剪刀"){ if(choice2 ==="paper"){ console.log("Scissors wins"); }其他{ console.log("Rock wins"); }¥b $ b } 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没有返回结果
发布评论