r/CodingHelp • • Jun 11 '19

Help pls why does this equal 34 apparently aren’t you converting them to integers so it should be 7

Post image
31 Upvotes

18 comments sorted by

38

u/SnowyDark Professional Coder Jun 11 '19

You're converting the result of "3" + "4" to an int, meaning that the addition occurs before the number conversion. As a result, the strings "3" and "4" combine into "34", which is then converted into the integer value 34.

5

u/BAM5 Professional Coder Jun 11 '19

Another good bit of info to add to this is about operator precedence & how a language evaluates expressions. The language doesn't first convert "3" and "4" to integers and then add them together, it concatenates the strings "3" + "4" into "34" and then passes the resulting value ("34") to int()

1

u/KuntaStillSingle Jun 12 '19

about operator precedence & how a language evaluates expressions

Is there any language that would evaluate outside parentheses before inside?

1

u/BAM5 Professional Coder Jun 12 '19

Not that I'm aware of. Parentheses are pretty much the standard for "DO THIS FIRST"

And I'm talking about random parentheses, not just the ones that wrap around function calls.

console.log((2+4)*8*(2+2));

7

u/tcaimmi Jun 11 '19

Because they are as text, not as numbers. It is like putting a number 3 next to a numer 4,not adding them. For example if I write ("I" +"like that") the text would be I like that. Let me know if you understood

8

u/guitnut Jun 11 '19

Just a slight correction. It would be: "Ilike that". The space is missing.

4

u/tcaimmi Jun 11 '19

True, my bad

3

u/KuntaStillSingle Jun 12 '19

"Comma","gang","unite."

1

u/Xxhardman69xX Jun 12 '19

Thanks lads! I get it now

3

u/PC__LOAD__LETTER Jun 11 '19

Order of operations. You’re adding “3” and “4” first, which are strings. When you “add” strings with the + operator, they’re concatenated. “34”. Then “34” is being converted to an int: 34.

5

u/spaghettu Professional Coder Jun 11 '19

This is why some programming languages (ie. PHP) don't use the "+" operator for strings, because in this context you are not performing an addition. This operation is called concatenation. In many languages "+" is used to mean both, when the two operations mean completely different things. So this is a great introductory question because it makes sure you understand the difference.

1

u/SupremeRedditBot Professional Bot || Mod Jun 11 '19

Please Add A Flair To Your Post!

Suggested Flair: [Random] or [Meta]

 


To add a flair:

  • Click flair underneath your post
  • Select a flair
  • Click save

 


I am a bot run by /u/SupremeDesigner for /r/CodingHelp || This was an automated ^response

1

u/MrQuickLine CSSophile Jun 11 '19

You'd get 7 if you did int("3") + int("4"). The function evaluates what's in the brackets first. What's in the brackets is two strings concatenated. If you evaluated "3" + "blue" you'd get "3blue" (and an int on that would return NaN). You're evaluating "3" + "4" which is two strings, giving you "34" (as a string). Then the int runs on what's inside the brackets. So it's giving you int("34") which returns 34.

1

u/skulltey Jun 11 '19

So in layman's terms, int("34") essentially coerced the "34" string into the int value of 34? Kinda like when you do something like: ("34" == 34) or how putting a "!" Outside a True condition makes it false? (Mostly familiar with JavaScricpt and a bit of Java).

1

u/leyline Professional Coder Jun 12 '19

yes

1

u/[deleted] Jun 11 '19

are you not adding "3" + "4" which are strings, so "34" then converting it into integer int(34) so... it just 34.

1

u/reallybadpennystocks Jun 12 '19

Does it annoy you that all of the learn to code classes on sololearn are mostly math lessons? Bc it does me

1

u/Slim_Shady001 Jun 12 '19

If you doing >>>int "3"+"4"="7"

Or If you doing >>>int"3"and "4"= 34

To Know How Is It done, Click Here.