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

219
Visualizações
Javascript if-else statement not running correctly

I don't know why the if-else statement given in the question is function incorrectly. Even cashProvided.value >billAmount.value I keep getting the errorMsg which is being executed in the else statement. I tried using console.log() at different parts of my code to see where I'm going wrong but I am not quite able to figure it out myself.Any suggestion?enter image description here

const billAmount=document.querySelector("#bill-amount");
const cashProvided=document.querySelector("#cash-given");
const checkButton=document.querySelector("#Check");
const numberOfNotes=document.querySelectorAll(".no-of-notes");
const errorMsg=document.querySelector("#error-msg");
checkButton.addEventListener("click",clickHandler);
notes=[2000,500,100,20,10,5,1];

if(cashProvided.value >= billAmount.value)
        {
            console.log("inside if");
            console.log('Bill='+billAmount.value);
            console.log("Cash Provided="+cashProvided.value);
            var change=cashProvided.value-billAmount.value;
            console.log("Change="+change)
            for(i=0;i<notes.length;i++)
            {
                console.log(notes.length);
                console.log("entered for");
                var numOfNotes=Math.trunc(change/notes[i]);
                numberOfNotes[i].innerText=numOfNotes;
                change%=notes[i];
                console.log("new change "+change);
            }
        }
        else
        {
            console.log(billAmount.value);
            console.log(cashProvided.value);
            errorMsg.style.display="block";
            errorMsg.innerText="You want to wash plates or what? Gimme atleast "+billAmount.value;
        }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Values are coming as string. you should parse the value first.

second way: if(parseInt(cashProvided.value, 10) >= parseInt(billAmount.value, 10))

also you can set them as variables outside of the if statement.

Edit: in response for peter's comment. you should include a second argument intuo parseInt function. it will determine the base. i assume you are using 10 base.

about 4 years ago · Juan Pablo Isaza Relatório

0

The type of input's value is String, you need to parse it to integer or number first.

const cash = parseInt(cashProvided.value);
const bill = parseInt(billAmount.value);
//use cash and bill instead of cashProvided.value, billAmount.value
...
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