[英]Function with arguements and creating variables in javascript
I'm completely new to coding and I'm trying to create a function that will request user inputs, store the inputs in a variable to be created on the fly, and in the end output the conversion of the variable;我对编码完全陌生,我正在尝试创建一个 function 来请求用户输入,将输入存储在要动态创建的变量中,最后 output 进行变量的转换; my code is right below.
我的代码就在下面。 Thanks:
谢谢:
function dogHuman(yes, no) {
var humanAge = ((dogAge - 2) * 4) + 21;
var haveDog = prompt("Do you have a dog? " + "yes" + " or " + "no");
if (haveDog == yes) {
var dogAge = prompt("How old is your dog? ");
alert("If your dog were human, it would be " + humanAge + " years old");
} else if (haveDog == no) {
alert("Thank you for you attention");
} else {
var haveDog = prompt("Do you have a dog? " + "yes" + " or " + "no" + yes + no);
}
}
dogHuman();
The primary issue is with haveDog == yes
& haveDog == no
.主要问题是
haveDog == yes
& haveDog == no
。 Here yes
& no
are string.这里
yes
和no
是字符串。 So have to compare like 'haveDog === 'yes'
.所以必须比较像
'haveDog === 'yes'
。 No use of ===
.不使用
===
。 Secondly calculate humanAge
only if the user types yes
otherwise it will undefined
其次,仅当用户键入
yes
时才计算humanAge
否则它将undefined
function dogHuman(yes, no) { var haveDog = prompt("Do you have a dog? " + "yes" + " or " + "no"); if (haveDog === 'yes') { var dogAge = prompt("How old is your dog? "); var humanAge = ((dogAge - 2) * 4) + 21; alert("If your dog were human, it would be " + humanAge + " years old"); } else { alert("Thank you for you attention"); } } dogHuman();
You have a few problems,你有几个问题,
humanAge
assignment after the dogAge
prompt, because that needs to happen first.dogAge
提示之后移动humanAge
分配,因为这需要首先发生。 function dogHuman(yes, no) { var haveDog = prompt("Do you have a dog? " + yes + " or " + no); if (haveDog === yes) { var dogAge = prompt("How old is your dog? "); var humanAge = ((dogAge - 2) * 4) + 21; alert("If your dog were human, it would be " + humanAge + " years old"); dogHuman(yes, no); // Recursion } else { alert("Thank you for you attention"); } } dogHuman('yes', 'no'); // Pass your parameters into the call
I'm not sure what the question is, so instead I'll just go over everything I notice:我不确定问题是什么,所以我将只对我注意到的所有内容进行 go :
dogHuman
is called without any arguments, and looking at your code, it probably shouldn't have any arguments. dogHuman
在没有任何 arguments 的情况下被调用,并且查看您的代码,它可能不应该有任何 arguments。var humanAge = ((dogAge - 2) * 4) + 21;
var humanAge = ((dogAge - 2) * 4) + 21;
should go after dogAge
is first determined.dogAge
。haveDog
is taking a prompt
, you probably want to compare haveDog
to "yes"
rather than just yes
.haveDog
正在接受prompt
,您可能希望将haveDog
与"yes"
进行比较,而不仅仅是yes
。"Do you have a dog? " + "yes" + " or " + "no"
can be rewritten as just "Do you have a dog? yes or no"
"Do you have a dog? " + "yes" + " or " + "no"
可以改写为"Do you have a dog? yes or no"
Variables are set once;变量设置一次; they don't rerun what you set them to every time you run them;
每次运行它们时,它们都不会重新运行您设置的值; this misconception is common, and is where I think the early
humanAge
definition is coming from.这种误解很常见,这也是我认为早期
humanAge
定义的来源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.