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

122
Visualizações
What am I missing with these functions?

I am having troubles with the validating of functions. I need to validate my variables and make sure that you can not enter through with out giving a name or number. What am I doing wrong that this is not working?

<script>
    var candyName = false;
    var pricePerLb;
    var numOfLbs;
    var message;
    
    candyName = prompt("Enter the name of the candy: ");
    while(candyName == false){
        alert("Blank is invalid. Enter valid name: ");
        candyName = prompt("Enter the name of the candy: ");
    }

    pricePerLb = parseFloat(prompt("Enter the price per pound: "));
    

    numOfLbs = parseInt(prompt("Enter the average number of pounds sold in a month: "));
    

    if(numOfLbs > 2000 || pricePerLb > 10){
        message = candyName + " that costs $" + pricePerLb + " is sold, on average, " + 
numOfLbs + " lbs a month are a best-selling item.";
    }else{
        message = candyName + " are not a best-selling item.";
    }

    function validate(candyName){
    if(candyName != ""){
        valid = true;
    }else{
        valid = false;
        alert("Blank is invalid: ");
    }
    return valid;


    alert(message);

</script>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You have to check the return of your validate function instead of candyName as it is not false (it might be empty or undefined but not false), see this snippet:

    var candyName = false;
    var pricePerLb;
    var numOfLbs;
    var message;
    
    candyName = prompt("Enter the name of the candy: ");
    while(validate(candyName) == false){
        alert("Blank is invalid. Enter valid name: ");
        candyName = prompt("Enter the name of the candy: ");
    }

    pricePerLb = parseFloat(prompt("Enter the price per pound: "));
   

    numOfLbs = parseInt(prompt("Enter the average number of pounds sold in a month: "));
    

    if(numOfLbs > 2000 || pricePerLb > 10){
        message = candyName + " that costs $" + pricePerLb + " is sold, on average, " + 
numOfLbs + " lbs a month are a best-selling item.";
    }else{
        message = candyName + " are not a best-selling item.";
    }

    function validate(candyName){
    if(candyName != ""){
        valid = true;
    }else{
        valid = false;
    }
    return valid;

    }


    alert(message)

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