[英]unable to figure out what's wrong with the formula
var johnHeight = prompt('Enter the height');
var johnAge =prompt('Enter the age');
var ranaHeight = prompt('Enter the height');
var ranaAge =prompt('Enter the age');
var johnScore = johnHeight + 5 * johnAge;
var win = document.getElementById('winner');
if (johnScore > ranaScore) {
win.innerHTML = 'John is the winner with ' + johnScore + ' points';
}
else if (johnScore < ranaScore) {
win.innerHTML = 'Rana is the winner with ' + ranaScore + ' points';
}
else if (johnScore == ranaScore) {
win.innerHTML = 'It\'s a draw';
}
When I try to run the code, everything works fine but the formula ie JohnScore or ranaScore doesn't calculate correct values. 当我尝试运行代码时,一切正常,但公式即JohnScore或ranaScore不能计算出正确的值。 when I remove the prompt and give those variables a predefined values, it works fine.. can someone solve please.
当我删除提示并给这些变量一个预定义的值,它工作正常..有人可以解决。
You don't have a variable set up for ranaScore, and for the johnScore variable if you mean to add 5 to his height and then multiply by age which means it needs to be: 你没有为ranaScore设置变量,如果你想为johnScore变量添加5到他的身高,然后乘以年龄,这意味着它需要:
var johnScore = (johnHeight + 5) * johnAge;
if that's not what you need you may try: 如果那不是你需要的,你可以尝试:
var johnScore = johnHeight + (5 * johnAge);
The value from prompt is coming as string. 来自提示的值以字符串形式出现。 So to get the right calculation you need to convert that to number.
因此,要获得正确的计算,您需要将其转换为数字。 Notice
+
before prompt. 提示之前注意
+
。 Also you need to define ranaScore
. 您还需要定义
ranaScore
。
So if johnHeight is "10" and johnAge is "5", let's look how this was being evaluated by javascript: 因此,如果johnHeight为“10”且johnAge为“5”,那么让我们看看这是如何通过javascript进行评估的:
var johnScore = johnHeight + 5 * johnAge;
johnScore = "10" + 5* "5" which is "10" + "25" which is "1025".
var johnHeight = +prompt('Enter the height'); var johnAge = +prompt('Enter the age'); var ranaHeight = +prompt('Enter the height'); var ranaAge = +prompt('Enter the age'); var johnScore = johnHeight + 5 * johnAge; var ranaScore = ranaHeight + 5 * ranaAge; var win = document.getElementById('winner'); if (johnScore > ranaScore) { win.innerHTML = 'John is the winner with ' + johnScore + ' points'; } else if (johnScore < ranaScore) { win.innerHTML = 'Rana is the winner with ' + ranaScore + ' points'; } else if (johnScore == ranaScore) { win.innerHTML = 'It\\'sa draw'; }
<div id="winner"></div>
Looks like your variable ranaScore has not been defined, so you need this line. 看起来您的变量ranaScore尚未定义,因此您需要此行。
var ranaScore = rannaHeight + 5 * rannaAge;
Although in real life scenario you will need to build your JS function to receives arguments instead of using static variables eg see very basic example below. 虽然在现实生活中你需要构建你的JS函数来接收参数而不是使用静态变量,例如,参见下面的基本示例。
function comparevalues(arg1,arg2){
if (arg1 > arg2) {
win.innerHTML = arg1 + 'is greater than ' + arg2; //arg 1 wins
}
else if (arg1 < arg2) {
win.innerHTML = arg2 + 'is greater ' + arg1; //arg 2 wins
}
else {
win.innerHTML = arg2 + 'is equal to ' + arg1; //draw
}
}
comparevalues(4,5)
Always prompt returns string type , convert it into number using ParseInt method. 始终提示返回字符串类型,使用ParseInt方法将其转换为数字。 var johnHeight=parseInt(prompt('Enter the height'));
var johnHeight = parseInt(prompt('输入高度'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.