很难计算Javascript的总数
Having a hard time calculating totals Javascript
我似乎无法弄清楚为什么我不能计算积分并为每个用户总数保留它们。有什么建议吗?请帮忙。我能够使用提示等来构建这个游戏。但是试图在浏览器中将其变为现实一直是一个挑战。
这是代码:
function getName() {
var name = document.getElementById('fName').value;
// console.log(name);
$(".greetingPlayer").append("<h3 class='greeting'>Greetings " + name + "!</h3><br><h4>Please choose from the following Weapons...</h4>");
$(".statsA").css("display", "none");
}
var userChoice;
function choices(weapon) {
userChoice = weapon;
// console.log(userChoice);
$(".greetingPlayer").append("<h4 class='userChoice'>Users Choice : " + userChoice + "!</h4>");
var computerChoice = Math.random();
if (computerChoice <= 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
// console.log(computerChoice);
$(".greetingPlayer").append("<h4 class='computerChoice'>Computers Choice : " + computerChoice + "!</h4>");
function compare(choice1, choice2) {
var playing = true;
var human = 0;
var comp = 0;
while (playing) {
if (choice1 === choice2) {
console.log("The result is a tie!");
human = score;
comp = score;
} 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!");
}
} else {
console.log("That's not an option! Do it over " + name + " and try again!");
}
playing = false;
}
console.log("human : " + human);
console.log("comp : " + comp);
var numpoints = 0;
function points() {
if (++score >= str.length) {
numpoints++;
document.getElementByClassName("points").textContent = "Score: " + numpoints;
}
}
}
compare(userChoice, computerChoice);
}
// $(".greetingPlayer").append("<h4 class='userWeapon'> " + name + "! You win!</h4>");
我想
通了,我的范围都是错误的,我不知道为什么我试图在底部添加一个循环。然而,这似乎解决了...
function getName(){
var name = document.getElementById('fName').value;
// console.log(name);
$(".greetingPlayer").append("<h3 class='greeting'>Greetings " + name + "!</h3><br><h4>Please choose from the following Weapons...</h4>");
$(".statsA").css("display", "none");
}
// Here we declare a variable to represent a User's Choice outside of the function scope
var userChoice;
var computerChoice;
//These variables are created to keep score and are declared w/ zero for addition.
var compScore = 0;
var userScore = 0;
// Here we declare a function to compare a userChoice vs compChoice
var choices = function(weapon){
userChoice = weapon;
// console.log(userChoice);
computerChoice = Math.random();
// console.log(computerChoice);
if (computerChoice <= 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
$(".toolChoice").html(" " + userChoice + "!");
// console.log(computerChoice);
$(".toolChoiceB").html(" " + computerChoice + "!");
function compare(choice1, choice2){
if(choice1 === choice2){
console.log("The result is a tie!");
}else if(choice1 === "rock"){
if(choice2 === "scissors"){
console.log("rock wins!");
userScore++;
}else{
console.log("paper wins!");
compScore++;
}
}else if(choice1 === "paper"){
if(choice2 === "rock"){
console.log("paper wins!");
userScore++;
}else{
console.log("scissors wins!");
compScore++;
}
}else if(choice1 === "scissors"){
if(choice2 === "paper"){
console.log("scissors wins!");
userScore++;
}else{
console.log("rock wins!");
compScore++;
}
}else{
console.log("That's not an option! Do it over "
+ name + " and try again!");
}
}
// end of compare function
// compare function called
compare(userChoice, computerChoice);
// This consoles the score, use this as a start point for displaying the score.
// console.log("human : " + userScore);
// console.log("comp : " + compScore);
// This jQuery displays the score for each party
// This is the score for Humans
$('.scoreA').html(" " + userScore);
// This is the score for Computers
$('.scoreB').html(" " + compScore);
}
相关文章:
- 使用CSS或JavaScript计算分页符的数量
- JavaScript计算帮助(乘以时间)
- 四舍五入JavaScript计算
- 获取唯一值并使用javascript计算金额总和
- JavaScript计算项目的总价
- 如何用javascript计算从现在开始的时间
- 再次使用JavaScript计算两个日期之间的天数
- 删除项目时的JavaScript计算
- 需要JavaScript计算帮助
- JavaScript-计算两个日期之间的天数(包括结束日期)
- JavaScript:计算字符串中特定整数的实例
- 使用 JavaScript 计算并添加表单输入的值
- 使用 javascript 计算 Telerik RadDatePicker 日期差
- 使用 JavaScript 计算 Blob 的哈希值
- Javascript计算明年的同一个工作日
- 如何将我通过JavaScript计算的自定义金额作为金额发送到PHP表单
- javascript计算价格的小算法
- Javascript计算不适用于第二个文本字段
- javascript计算字段比较算法
- JavaScript计算不同于Firefox的图像尺寸(?)