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

147
Visualizações
Variables are not displayed correctly in the document

I have a question. I am doing a small exercise. I am starting. When I run the following code the nextAge appears in currentAge and I am looking for currentAge and nextAge to appear where I indicated in the document.write. I would appreciate it if you could correct me.

let yourName = prompt('What is your name?');
let currentAge = prompt('How old are you?');
let nextAge 
 
if(currentAge){
    nextAge = currentAge++;

    document.write(`Hi ${yourName}, You are ${currentAge} years old and next year you will be ${nextAge} years old`);
};
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

TLDR: you want nextAge = currentAge + 1

To understand this you need to understand how ++ works. When you use ++ you are simply telling the variable with ++ to increase by one. Secondly, the position of ++ in respect to the variable matters. If you have ++ leading the variable, you are saying increase variable VARIABLE_NAME by one FIRST and THEN do any other operations in this line. If you have ++ after the variable, you are saying do any other operations in this line FIRST and THEN increase the number by one.

So in your example

 nextAge = currentAge++;

actually translates to

nextAge = currentAge;
currentAge = currentAge + 1;

With ++ leading this

nextAge = ++currentAge;

turns into

currentAge = curentAge + 1;
nextAge = currentAge;
about 4 years ago · Juan Pablo Isaza Relatório

0

The ++ means increase value in the variable by 1. So what you are doing is increasing currentAge value and than returning that value into nextAge.

What you need is take value of currentAge and add to it 1 without changing currentAge:


nextAge = currentAge + 1;
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