简体   繁体   中英

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; 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 . Here yes & no are string. So have to compare like 'haveDog === 'yes' . No use of === . Secondly calculate humanAge only if the user types yes otherwise it will 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.
  2. Move the humanAge assignment after the dogAge prompt, because that needs to happen first.
  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:

  1. dogHuman is called without any arguments, and looking at your code, it probably shouldn't have any arguments.
  2. Javascript (as, indeed, most languages) does things in order, so var humanAge = ((dogAge - 2) * 4) + 21; should go after dogAge is first determined.
  3. Since haveDog is taking a prompt , you probably want to compare haveDog to "yes" rather than just yes .
  4. "Do you have a dog? " + "yes" + " or " + "no" can be rewritten as just "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.

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.

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