Why my number couldn't add up if I separated it in a conditionals?

As title, if both booleans were true, it would not print out the number + 1000.
if (registerEarly && age >= 18){

raceNumber + 1000;

console.log(raceNumber);
}

But the codes will work, I wonder what’s the difference?
if (registerEarly && age >= 18){

console.log(raceNumber + 1000);
}

So I’m not sure if this is the solution/guide you need, but I think you might have to store raceNumber + 1000 in your code. I mean, how else will the computer know to add raceNumber to 1000 and return (:wink: ) the value?

I hope this helps…

As in, re-assign.

raceNumber = raceNumber + 1000

since we want the new value to be assigned to the old variable.

1 Like

Thank you for the correction! I will keep that in mind. I was thinking about along the lines of
return raceNumber + 1000 , but I think you clarified we want to reassign a value, not store it. :+1:

1 Like

We’re often told that this is storing a value in a…

a = 42

The truth is we don’t look after storage, the computer does. We only give stored items a reference so that the value can be polled, queried or set. The value will remain in memory as long as it has a reference. Change the reference to a new value and the old value gets garbage collected, again by the computer (OS, actually), not us.

Ah thank you for explaining this process! It’s interesting… but now that I think about it, that is true… for instance, reassigning a = 42 to a = 50 and the 42 is “forgotten” unless we restore it :thinking:

I suppose return would work in a similar way “behind-the-scenes”, then. I mean, I usually think of return as -not a variable- but a container that outputs data we need, we just can’t see it. (So in a way it is like a variable :joy:)

const add = (a, b) => a + b;    // return pair sum

const reduce = arr => {
    let x = arr.slice()         // return a shallow copy
    let y = 0
    while (x.length > 0) {
        y = add(y, x.pop())     // return last value removed
    }
    return y                    // return the array sum
}

console.log(reduce([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
55

The above demonstrates return in multiple ways. There are four functions, two being methods:

  • add()
  • reduce()
  • Array.slice()
  • Array.pop()

.slice() and reduce() get called once, with an array of numbers as the context, and argument respectively. To preserve the original array we take a shallow copy of it which we are now free to destroy.

The while loop keeps calling the add() function as long as there are still numbers in the array. In the argument of that call is a call to the .pop() method which takes a number off the end of the array and returns it. That value, and the current value of y are passed to add() which returns the pair sum.

At the end of loop y is returned to the caller, which is the console.log() method. Our call is the argument to that function.

In none of the above is return anything akin a variable since there is no assignment operation involved. The return keyword acts as a an endpoint indicator of a thread that leads back to the caller. The return value follows that thread from function scope to caller scope.

Ohh ok, this explains it very clearly. Thank you for the example and help to let me see the difference! I will keep this in mind moving forward!

1 Like