Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
How do I stop when my first IF is met and not have the two welcome alerts after the first IF is met

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!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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.
about 4 years ago · Juan Pablo Isaza Relatório

0

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.")
    }
    }
about 4 years ago · Juan Pablo Isaza Relatório

0

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>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda