Modifier Math explainer


#21

Thank you very much @Ahsen and @Grasshopper_Heather!
It’s not easy but I do enjoy a lot! Thanks again!


#22

You welcome, it gets easier over time. The more you practice and even use in real life the easier the task let it be coding to whatever. The key here is you are enjoying it always remember that. :slight_smile:


#23

Hi Grasshopper _Heather can you explain the use of + symbol in this case in detail


#24

Hey @SIJU,

Of course!

In JavaScript, the + operator can do two things: combine two numbers together (like the + symbol does in mathematics), and concatenating two strings together.

For example, we could have code like:

var myNumber = 5
var myStatement = myNumber + ' is equal to five'
print(myStatement)

The variable myStatement combines a number (the value from myNumber) with the string ' is equal to five'. JavaScript recognizes these are different datatypes, and so converts the value from myNumber from a number to a string. So the expression would actually look something like '5' + ' is equal to five'. And the + symbol combines these two strings to create '5 is equal to five'.

We can also create the same output as above using less code, by getting rid of the myStatement variable. Like this:

var myNumber = 5
print(myNumber + ' is equal to five')

I hope this helps!
H :slight_smile:


The Final Ascent explainer
Open The Lock explainer
#25

The challenge I have is that sometimes I’m not sure what exactly the exercise is asking for.


#26

Could you please help me? I still don’t get what am I doing wrong.


#27

Hey @Javid_Allahverdiyev,

Looks like you’ve given yourself extra work: you’ve got too many lines of code.

Try resetting your code by tapping on the button with arrows in a circle (the one that looks a little like the recycle symbol); then edit (rather than adding to) the code to replace the 0's with a number of 1 or higher.

H :slight_smile:


#28

Thanks Heather!
I thought I needed to start the code from scratch, it turns out it was easier than I thought.


#29

I am stucked here. All data entered is as per instruction .Plz someone help me.


#30

Hi @Dnyanehz,

Could you share a screenshot of your code? I can take a look and hopefully spot how to help you.

Thanks,
Heather


#31

Thank you. I fixed it.


#32

Hi, I understand what it’s looking for and completed the exercise, I just don’t fully understand why if x=5 and you’re updating that to a new value of x=x+5 why it’s not 5=5+5. Why does the second x in the equation retain the last value for x, but the first one doesn’t?


#33

Hi @Murcielago,
The important thing to keep in mind is that in JavaScript, = does not mean what it means in mathematics. In JavaScript and many other programming languages, it indicates that the calculated value of the expression on the right is assigned to the variable on the left.

Thus, in x=x+5, x+5 evaluates to 5+5, which evaluates to 10, which is then assigned to the variable on the left, which is x.

Does that help? I think maybe the tutorial needs to explain more thoroughly that the various symbols do not have quite the same meaning as in mathematics.

–Scott.


#34

Hey @Murcielago,

In addition to what @Scott_McNay has explained, it may be useful to walk through each element as if we are talking to the computer. So let’s break it down:

x = : this part is telling the computer that we want to assign a new value to x. Whenever you see the = symbol it means that the variable (or identifier) on the left is going to be assigned a new value.

x + 1 : this is on the right side of the assignment symbol, meaning it is the new value that will be given to x. At this point, x still has it’s old value. The computer will look ‘up’ to see what x has been holding to date, and then add 1 to it.

Hopefully this helps!
H :slight_smile:


#35

I put in your solution exactly and I still get errors from your app that I can’t complete this puzzle


#36

Are you still stuck on this puzzle? Make sure you’re using num for numbers and str for words. If there are quote marks around a number, then it will be seen as a keyboard character, not an amount.

You can also upload a picture of your code here to get another look.

–Frankie


#37

Hrmm… It is confusing how the language always appears to be mathematical but is actually a combination of directive shorthand and math. Instinctively I look at it and think “x” does not equal “x + 5”… (How is it not a circular reference? Is “x =” of “x = x + 5” happening independently of “x + 5”? Wouldn’t it make more sense to write “x + 5 = x” if the goal is to update the value of x? How does the print function portion know to show the value of x rather than displaying “x”?)
Will be interesting to see how complex math functions are handled later on (if those are in the program). Thanks for your explanation.
EDIT: I see you guys answered my questions above. Sorry, disregard my post


#38

Hi @Sportyyyy,

print x will always only ever print the contents of the variable named x.

print "x" will always only ever print the literal string shown within the quotes: “x”.

The difference is that one has quotes and the other doesn’t.

Each language has it’s own rules. In most modern languages, you can use this:

a = b = 2

to determine if b equals 2 and assign true or false to a.

But, I recall seeing code for what I think was an old mainframe version of BASIC in which the value of 2 would be assigned to the variable b, then the value of the variable b (which was now 2) would then be assigned to the variable a.


#39

As a clarification to future readers:

In JavaScript and most languages a single equal sign = is the assignment operator. That means “make one side equal to the other side” (usually, change the left side to match the right side).

In JavaScript and many other languages, multiple equal signs, == or ===, is the equality operator. That means “check if one side is already equal to the other side”.

That means a = b = 2 will be interpreted as “set b to 2, and then set a to the value of b which is 2”. Which makes a and b have a value of 2. The code a = b === 2 can be rewritten as a = (b === 2), and you can break it up into two parts:

  • b === 2, “Is the variable b storing the value 2?”. That is either true or false.
  • a = (either true or false depending on if b was equal to 2)

@Sportyyyy
Here is a chart that lists the order that each operator is executed. If you remember the order of operations and PEMDAS from arithmetic, this is the same thing except with more operators included. You’ll notice that assignment = is one of the last operations to happen. That means you’ll usually have all the math compress down to a simpler form and that final value will be assigned to the variable. There’s also a column called “Associativity” which shows the order if there are multiple operators with the same precedence/ranking. In JavaScript and nearly all languages, assignment evaluates the right side and assigns that value to the left side.

–Frankie


#40

Im very confuse help :hushed: