[SOLVED]why I'm getting this weird output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

[SOLVED]why I'm getting this weird output?

may somebody explain why the output is not 42? https://code.sololearn.com/cchcznzqzT6n/?ref=app

25th May 2021, 8:55 AM
hamid
hamid - avatar
15 Answers
+ 5
042 is octal base (8) here not decimal base(10)
25th May 2021, 9:16 AM
TOLUENE
TOLUENE - avatar
+ 3
well, if you know the four basic mathematical operations, you are able to understand at least how decimal (base 10) works ;) if you write 142, you know that you use digit in range 0..9 (10 different digits/signs) because we have 10 fingers, so that's the more human logical base to use, as we can count with our fingets... now, the most right digit is called the unit (correspond to one digit), the next one the ten, and next one the hundred... 1 is one, 10 is ten, 100 is hundred, so you could see that we multipli by 10 each time we shift digit by one place to the left (by appending a zero at right): unit is digit*1, ten is digit*10, hundred is digit*10*10. raising a number to a power mean multiplying that number by itself power times... 10*10 = 10 raised at power 2, 10 is 10 raised at power 1, and 1 is 10 raised at power 0. Could you say at wich power is raised thousand (1000)? I'm quite sure you can see that's 10*10*10, so that's 10 raised to power of 3 ;) Are you getting the principle?
25th May 2021, 3:52 PM
visph
visph - avatar
+ 3
visph yes thoroughly
25th May 2021, 4:55 PM
hamid
hamid - avatar
+ 3
so, math operator for "raised to power" is ^ (almost, in real math equation, the power follow the number superscripted), but in most of programming languages having one it is **...) now 142, can be decomposed to 1*(10^2) + 4^(10^1) + 2*(10^1) = 1*100 + 4*10 + 2*1 = 100+40+2 in other bases, the principle remain the same: the base number describe how many signs are available for "digits"... in base 2 (binary): 0 or 1 (2 'digits') in base 16 (hexadecimal): 0..9 + a..f (16 'digits' a=10, b=11,... f=15) in base 8 (octal, less used, but popular in early years of computers): 0..7 (8 'digits') octal numbers can be written in literal notation of most of programming language by prefixing them with a zero. and 042 can now be decomposed as '42' in octal / base 8, and computed as 4*(8^1) + 2*(8^0) = 4*8 + 2*1 = 32 + 2 = 34 (decimal)
25th May 2021, 5:12 PM
visph
visph - avatar
+ 3
visph yeah, that works. I'm really really thankful for the quite clear explanation. I finally got it.
25th May 2021, 6:58 PM
hamid
hamid - avatar
+ 2
because 042 is octal (base 8) litteral number... and '42' in octal is (4*8)+2 == 34 (decimal). for decimal, don't use 0 prefix: 42.
25th May 2021, 9:16 AM
visph
visph - avatar
+ 2
Md Sayed and visph thanks for answering. I changed the code a little bit and it's getting more confusing. can you explain more clearly?
25th May 2021, 10:41 AM
hamid
hamid - avatar
+ 2
you said that 42 in octal is (4*8)+2 but when you change the number to 42 it prints 42 instead of 34
25th May 2021, 10:42 AM
hamid
hamid - avatar
+ 2
I putted quote to '42' to differenciate from a literal in code (meaning the number composed by 4 and 2 digits)
25th May 2021, 10:44 AM
visph
visph - avatar
+ 2
in base 10, each digit is multiplied by 10 raised to the position of digit from the right (starting at 0) and then all added, in base n, each digit is similarly multiplied by n raised to the power of the digit position ;)
25th May 2021, 10:47 AM
visph
visph - avatar
+ 2
hamid no problem for delay: each of us could have other things to do as well as time to digest ;) however, answer here cannot be very long, so feel free to ask for clarification if needed ^^ (could I ask what's your math knowledge level? I mean: are you very young, few young, old, quite old, very old?)
25th May 2021, 1:02 PM
visph
visph - avatar
+ 2
visph just the way I ask everyone will finally figure that out😂(😪). unfortunately few young or to be more honest very young. as one of my posts shows how much I hate math and I always tried to understand and do things without mixing them up with math while sometimes it's pure math and bypassing them is impossible. I really think that my brain doesn't have a part for analyzing and understanding math(LoL) and the four basic mathematical operations that I know, is actually something that has happened as time went on.(yep that was my math level)
25th May 2021, 3:32 PM
hamid
hamid - avatar
+ 1
parseInt doesn't imply base 10 (decimal); it treat your string number as literal number, so 042 is still octal and 42 is still decimal
25th May 2021, 10:43 AM
visph
visph - avatar
+ 1
visph well it's seems you explaining clearly I don't know why I don't follow😬 so please consider these delays in replying as digesting process I need to search more.
25th May 2021, 12:53 PM
hamid
hamid - avatar
0
I ,don't know
26th May 2021, 7:41 AM
Tiger Coder
Tiger Coder - avatar