简体   繁体   English

无法弄清楚公式有什么问题

[英]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.

相关问题 无法弄清楚这个链表循环方法有什么问题 - Unable to figure out what's wrong with this Linked List Cycle approache 无法弄清楚这个简单的JS代码出了什么问题 - Can't figure out what's wrong with this simple JS code 我不知道怎么了 - I can't figure out what's wrong 我想为不和谐做一个随机问题机器人,但我无法弄清楚什么是错的 - I wish to make a random question bot for discord, but I can't figure out what's wrong 有人可以帮我弄清楚我的代码有什么问题吗? 它将 RNA 序列翻译成蛋白质 - Can someone help me figure out what's wrong with my code?. It translates RNA sequences into proteins 任何人都可以帮我弄清楚这个 javascript 有什么问题吗? - Can anyone help me figure out what's wrong in this peice of javascript? 反应原生渲染按钮列表无法找出问题所在 - react native rendering a list of buttons can't figure out what's wrong 我正在解决一个实践竞争性编程问题,但我无法弄清楚出了什么问题 - I am solving a practice competitive programming question and i am unable to figure what's going wrong 我正在尝试找出此Javascript代码出了什么问题 - I am trying to figure out what is wrong with this Javascript code 无法弄清楚我的JavaScript代码出了什么问题 - Can't figure out what is wrong with my JavaScript code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM