Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!