So I'm trying to create a prompt for the user to manually input scores. Issue is, the math involved isn't working like it should and treating the problem as a multiplication problem than a sum. Any idea if I have a bug or something of error on my end? Here's my whole code:
//Code Challenge 02
//Johns team will start on the calculation.
johnTeam = 89 + 120 + 103;
newScore = johnTeam/3;
johnTeam = newScore;
//For the sake of mathematic sanity, lets put newScore to 0.
newScore = 0;
console.log(johnTeam + " scored this much in average.\n");
//Next is Mikes Team
mikeTeam = 116 + 94 + 123;
newScore = mikeTeam/3;
mikeTeam = newScore;
//Reset newScore
newScore = 0;
console.log(mikeTeam + " scored this much in average.\n");
//Lastly, Mary's Team
maryTeam = 97 + 134 + 105;
newScore = maryTeam/3;
maryTeam = newScore;
//Reset newScore
newScore = 0;
console.log(maryTeam + " scored this much in average.\n");
//Calculate on who won.
console.log("Round 1: Who won?\n");
//Lets make a big huge enormous if and else statements revolving around these teams.
if(johnTeam > mikeTeam && johnTeam > maryTeam)
{
console.log("Johns team wins!");
}else if(mikeTeam > johnTeam && mikeTeam > maryTeam){
console.log("Mikes Team wins!");
}else if(maryTeam > johnTeam && maryTeam > mikeTeam){
console.log("Mary's Team wins!");
}else if(johnTeam === mikeTeam && johnTeam > maryTeam){
console.log("John and Mike both tied!");
}else if(johnTeam === maryTeam && johnTeam > mikeTeam){
console.log("John and Mary have both tied!");
}else if(mikeTeam === maryTeam && mikeTeam > johnTeam){
console.log("Mike and Mary have both tied!");
}else{
console.log("Everybody is a winner! Wowzerz!");
}
//*************************************************** //Now that is over, ready for round 2? Lets try something more realistic, and not so generic.
//Reset all scores!
johnTeam = 0;
mikeTeam = 0;
maryTeam = 0;
console.log("\nInsert Johns score per question.");
var a = prompt("Insert first score.");
console.log(a);
var b = prompt("Insert second score,");
console.log(b);
var c = prompt("Insert third score.");
console.log(c);
johnTeam = a + b + c;
newScore = johnTeam/3;
johnTeam = newScore;
newScore = 0;
console.log(johnTeam + " scored this much in average.");
`
Objective: To add up three scores, then divide for the average score. Then decide which team has the higher score.
Issue: First part is fine. The prompt part is treating the equation like a multiplication problem.
hint()函数返回一个字符串,因此您必须像这样解析它:
parseInt('your string', 10)
Prompt returns a string need to parse the results into integers to do the math on them:
Result is a string containing the text entered by the user, or null.
johnTeam = 0; mikeTeam = 0; maryTeam = 0; console.log("\\nInsert Johns score per question."); johnTeam = parseInt(prompt("Insert first score.")); console.log(johnTeam); mikeTeam = parseInt(prompt("Insert second score,")); console.log(mikeTeam); maryTeam = parseInt(prompt("Insert third score.")); console.log(maryTeam); newScore = (maryTeam + mikeTeam + johnTeam) / 3 console.log(newScore + " scored this much in average.");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.