简体   繁体   English

Function 有争论并在 javascript 中创建变量

[英]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.这里yesno是字符串。 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,你有几个问题,

  1. Pass the appropriate parameters into the function call.将适当的参数传递给 function 调用。
  2. Move the humanAge assignment after the dogAge prompt, because that needs to happen first.dogAge提示之后移动humanAge分配,因为这需要首先发生。
  3. Make sure not to quote your variables确保不要引用你的变量

 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 :

  1. dogHuman is called without any arguments, and looking at your code, it probably shouldn't have any arguments. dogHuman在没有任何 arguments 的情况下被调用,并且查看您的代码,它可能不应该有任何 arguments。
  2. Javascript (as, indeed, most languages) does things in order, so var humanAge = ((dogAge - 2) * 4) + 21; Javascript(事实上,大多数语言)按顺序做事,所以var humanAge = ((dogAge - 2) * 4) + 21; should go after dogAge is first determined.应该先确定 dogAge 后的dogAge
  3. Since haveDog is taking a prompt , you probably want to compare haveDog to "yes" rather than just yes .由于haveDog正在接受prompt ,您可能希望将haveDog"yes"进行比较,而不仅仅是yes
  4. "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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM