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`);
};
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;
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;