Javascript没有返回结果

编程入门 行业动态 更新时间:2024-10-28 06:28:21
本文介绍了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没有返回结果

本文发布于:2023-10-26 12:41:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530174.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!