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

136
Visualizações
Print one message for specific condition, different message for everything else

My homepage prompts you to enter your name upon entry, but I want it to print a special message when i enter my nickname. Instead, it prints the same message every time.

window.onload = function namePrompt() {
  let person = prompt("Please enter your name", "");
    if (person != null) {
    document.getElementById("name").innerHTML =
    "welcome, " + person + ", to the pyrogaming/covid2009 homepage!";
  }
  else if (person = pyro || Pyro || PYRO) {
    document.getElementById("name").innerHTML =
    "hey, me! hope you've been making substantial changes!";
  }
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

person = pyro || Pyro || PYRO is not valid JS

There are three errors. = is assignment, not comparison and you would need quotes and test each one with ===

person === "pyro" || person ==="Pyro" || person === "PYRO"

Instead use toLowerCase and a ternary. In any case stay DRY (Don't Repeat Yourself)

I also recommend eventListener instead of onload

window.addEventListener("DOMContentLoaded", () => {
  const person = prompt("Please enter your name", "");
  if (!person) return; // empty string or null
  const text = person.toLowerCase() === "pyro" ? 
    "hey, me! hope you've been making substantial changes!" : 
    `welcome, ${person}, to the pyrogaming/covid2009 homepage!`;
  document.getElementById("name").innerHTML = text;
})
<span id="name"></span>

about 4 years ago · Juan Pablo Isaza Relatório

0

  1. You need to first check the name if it's yours and after that display the default message
  2. When comparing, use ===, not = - this one is "assign value", not comparing
  3. Use string values instead of just PYRO
  4. I'd also suggest to use toLowerCase method, so you don't need to worry about input case. "Pyro", "PYRO", "pyro" - they all will be the same for comparison

window.onload = function namePrompt() {
  let person = prompt("Please enter your name", "");
  if (person != null) {
      if(person.toLowerCase() === 'pyro') {
        document.getElementById("name").innerHTML =
          "hey, me! hope you've been making substantial changes!";
      } else {
        document.getElementById("name").innerHTML =
          "welcome, " + person + ", to the pyrogaming/covid2009 homepage!";
      }
  }
}
<div id="name"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

window.onload = function namePrompt() {
  let person = prompt("Please enter your name", "");
  var msg
  if (person != null) {
    if (person.toLowerCase() == "pyro") {
      msg =
        "hey, me! hope you've been making substantial changes!";
    } else {
      msg = "welcome, " + person + ", to the pyrogaming/covid2009 homepage!";
    }
    document.getElementById("name").innerHTML = msg
  }
}
<div id="name"></div>

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