Why can't it be the other way around?

Why can’t the answer be:

processArg(7) = processed; ?

  **Your code so far**

// Setup
let processed = 0;

function processArg(num) {
return (num + 3) / 5;
}

// Only change code below this line
processed = processArg(7);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36

Challenge: Assignment with a Returned Value

Link to the challenge:

1 Like

Before we give you an answer, try to explain to us what you think the above line should do. Maybe when you try to explain it you will see why you can’t do this.

3 Likes

Yeah, what bbsmooth said.

But just to be clear, the = operator is different in JS than it is in math.

In math it is saying “the thing on the left is the same on the right”. In math, it is commutative, that is, if a = b then b = a.

But in JS, the = means “take whatever is on the right side and store it in the variable of the left side”. In the original code, it is saying “take whatever processArg(7) evaluates to and store it in the variable processed”. How would the inverse of that work? How would you “take the variable processed and store it in whatever processArg(7) evaluates to”? How do we store a variable in a value? It’s supposed to be the other way around.

2 Likes

To make the issue even more clear, if we have:

name = "Bob";

That is saying “store the string literal ‘Bob’ in the variable name.”

If it was commutative, it would be the same as saying, “store the variable name in the string literal ‘Bob’”. That is not the same thing and I’m not even sure what that would mean, in terms of memory.

1 Like

That was my mistake - I am still applying math rules to JS. Thank you!

The maths rules are the same, but = (single equal character) is already taken as an operator (for variable assignment), so is not available to be used in JS as the equivalent common maths symbol for equality. So “equality” is == and what is basically “exact equality” (triple barred equals, although it’s more specific than that in JS) is ===

2 Likes

Yes, that’s basically the idea. I think that there is also an additional subtle difference. In math, a = b is an assertion, saying that these are equal. In JS, a === b isn’t really an assertion, but is a question, “True or false? These things are equal?” and the whole thing will evaluate to true or false.

It’s a subtle difference, at least as I see it.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.