Home > other >  How do I stop when my first IF is met and not have the two welcome alerts after the first IF is met
How do I stop when my first IF is met and not have the two welcome alerts after the first IF is met

Time:05-19

var guestlist = ["Angela", "Jack", "Thu", "Amy", "TC"];

var name = prompt("What is your name?");

if (name === "TC") {
  alert("Welcome "   name   " to the party!");
  return;
} else {
  var firstChar = name.slice(0, 1);
  var upperCaseChar = firstChar.toUpperCase();
  var lastChar = name.slice(1, name.length);
  var lowerCaseChar = lastChar.toLowerCase();
  var completeChar = upperCaseChar   lowerCaseChar;
  var name = completeChar;
}

if (guestlist.includes(name)) {
  alert("Welcome "   name   " to the party!");
} else {
  alert("Sorry, "   name   ". Unfortunately, you are not on the list.")
}

I just started javascript. So, I am trying to see how I can get the first IF to stop. Thank you, guys!

CodePudding user response:

Try changing your code to this.

const guestlist = ["Angela", "Jack", "Thu", "Amy", "TC"];

const name = prompt("What is your name?");

const firstChar = name.slice(0, 1).toUpperCase();
const lastChar = name.slice(1, name.length).toLowerCase();
const completeChar = firstChar   lastChar;

if (name === "TC") {
  alert("Welcome "   name   " to the party!");
} else if(guestlist.includes(completeChar)){
  alert("Welcome "   completeChar   " to the party!");
}else{
  alert("Sorry, "   completeChar   ". Unfortunately, you are not on the list.")
}

Now lets go over it bit by bit on why and how it's been changed:

  1. Change all of your vars to consts according to the new ECMA coding standards.

  2. You can stack methods and instead of having:

    var firstChar = name.slice(0, 1); var upperCaseChar = firstChar.toUpperCase();

You can simply do it in a single line like this: const firstChar = name.slice(0, 1).toUpperCase();

  1. Go check out THE MDN WEBDOCS for If...else statements and control, in order to gain better understanding how to write if...else statements that have several conditions inside.

CodePudding user response:

Just put your other if statements inside your first if's else statement and at the end of 'if' you don't need to use return, its useless (unless there is more code to it and your code is inside some function that must break at the point of return to stop execution)

var guestlist = ["Angela", "Jack", "Thu", "Amy", "TC"];
    
    var name = prompt("What is your name?");
    
    if (name === "TC") {
      alert("Welcome "   name   " to the party!");
      //return; no need for this
    } 
    else {
      var firstChar = name.slice(0,1);
    var upperCaseChar = firstChar.toUpperCase();
    var lastChar = name.slice(1,name.length);
    var lowerCaseChar = lastChar.toLowerCase();
    var completeChar = upperCaseChar   lowerCaseChar;
    var name = completeChar;
    
    if (guestlist.includes(name)) {
      alert("Welcome "   name   " to the party!");
    } 
    else {
        alert("Sorry, "   name   ". Unfortunately, you are not on the list.")
    }
    }

CodePudding user response:

You can use the stop function to stop in the first IF!

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</html>
<script>
  var guestlist = ["Angela", "Jack", "Thu", "Amy", "TC"];

  var name = prompt("What is your name?");

  if (name === "TC") {
    alert("Welcome "   name   " to the party!");
    stop;
  } else {
    var firstChar = name.slice(0, 1);
    var upperCaseChar = firstChar.toUpperCase();
    var lastChar = name.slice(1, name.length);
    var lowerCaseChar = lastChar.toLowerCase();
    var completeChar = upperCaseChar   lowerCaseChar;
    var name = completeChar;
  }

  if (guestlist.includes(name)) {
    alert("Welcome "   name   " to the party!");
  } else {
    alert("Sorry, "   name   ". Unfortunately, you are not on the list.");
  }
</script>

</body>

  • Related