Freecodecamp: Storing Values with the Assignment Operator

Created on 25 Jan 2017  路  5Comments  路  Source: freeCodeCamp/freeCodeCamp


Challenge Name

Storing Values with the Assignment Operator

Issue Description

Do not change code above the line
a should have a value of 7
b should have a value of 7
a should be assigned to b with =

Browser Information

In JavaScript, you can store a value in a variable with the assignment operator.

myVariable = 5;

Assigns the Number value 5 to myVariable.

Assignment always goes from right to left. Everything to the right of the = operator is resolved before the value is assigned to the variable to the left of the operator.

myVar = 5;
myNum = myVar;
Assigns 5 to myVar and then resolves myVar to 5 again and assigns it to myNum.

Instructions
Assign the value 7 to variable a.

Assign the contents of a to variable b.

  • Browser Name, Version:
  • Operating System:
  • Mobile, Desktop, or Tablet:

Your Code

// Setup
var a;
var b = 2;

// Only change code below this line

Screenshot

image

Most helpful comment

@sanjayk722 Please read the question carefully and code accordingly.The question is correct.

All 5 comments

What is the issue that you are facing?

The issue tracker is for reporting bugs only.

Please use the chat rooms:

or try looking through our forum for help with a specific challenge.

Happy Coding!

What's the issue here? As I see in the screenshot, you need to follow the instructions and then run tests.

@sanjayk722 Please read the question carefully and code accordingly.The question is correct.

@sanjayk722 some challenges are tricky, and are set up to make think harder, always reread the introduction, instructions, comments, and ask for help and a second opinion in the freecodecamp chatrooms!

This challenge works, although i can understand the confusion...

Also returning the answer does not pass the challenge, because it is not specific to the instructions

for example

return c;
/*does not pass the challenge*/

If you're teaching someone to code, telling them how to assign a variable in one exercise, and then later in the exercise presenting unclear terms telling them to assign a set of variables that makes no logical sense to a newbie your teaching style is at fault, not the newbie's understanding of the instructions. It's akin to saying, hear's how you crawl; now run. Please rethink the instructions.

// Setup
var a;
var b = 2;

// Only change code below this line

@sanjayk722 Understand that b already has a value of 2. While to someone new to code it would make sense to do it this way from a grammatically logical point of view because that is what the instructions directly state, it is a trick.

a = 7;
b = 7;
a = b;

Programming has its own differing sense of logic in order to remain as resource efficient as possible. With this mindset, you have to count Variable b already equals 2. Working with the unchanging principle that b already has a value of 2, you must look deeper from a mathematical perspective at how JS resolves assignments.

"Assignment always goes from right to left. Everything to the right of the = operator is resolved before the value is assigned to the variable to the left of the operator."

Keep in mind code processes from top to bottom, left to right. You will have to put some assignments above others for this to make sense. You may also have to ensure some assignments are on the left side of the "=" and not the right for this to make sense.

Good luck, you got this.

Was this page helpful?
0 / 5 - 0 ratings