Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am currently following the w3schools tutorial on JavaScript and am getting REALLY stuck on the Numbers/Number Methods and Maths pages.

I really do not understand the maths to do the sums or how or why the answers come up as these long and complected numbers. Perhaps I am missing the obvious and you find the answer easily, its obvious, or its simple maths, or maybe it is something which is saying "this is what JavaScript can do", but you personally are not meant to be able to do it... I don't know.

The ones I have not been able to do are:

- Hexadecimal numbers , the answers 128, 80 and 1000000 how do you get that?

- The toExponential Method. (returns 9.66e+0? How did the code do that?)

- The tofixed() Method - (I just don't understand there must be some pattern or multiplying or something but i'm just not getting it.)

- toPresision Method... no idea. (I know I am sounding stupid now.)

and the Number() Method - Looks like it is logical and should be easy but again I have no idea what to do.. :(

- The parselent method... 10 = 10, yes, but 10.33 = 10? 10 20 30 = 10? I'm sorry, what..?

(It is the same problem believe as with the parseFloat() Method.)

So, yes, it seems like a lot but I am sure there are ways of explaining some things which will make lots of the others suddenly make sense. If you need to see the full example and code the go to www.w3schools, JavaScript. The very first problem is on the subject "JS Numbers" and all the others are on the page "JS Number Methods".

A big thank you to anybody is able to help me!! I appreciate it, please do your best!

From me.

What I have tried:

Looking at everything for hours and getting no great idea, yahoo answers, asking my family for ideas... nothing. :(
Posted
Updated 2-Aug-16 8:46am

1 solution

The number 128 is in our "natural" base: base 10.
Which basically means that it is really:
1 * 10^2 + 2 * 10^1 + 8 *10^0

Or
1 * 100 + 2 * 10 + 8 * 1
Because that is what "base 10" means - you have ten distinct digits (0 to 9) and you get "bigger numbers" you multiply by the base value.

But that's not the only way to work with numbers. You can use any positive integer as a base, except 0 and 1 (that's not actually true, you can use non-integral bases or negative bases but that gets well beyond the scope of a little text box so I'm goign to ignore those completely).
Basically, all western math is positional - the number of digits to the right of the digit you are talking about is the power to which you raise the base in order to to get the "complete" value for a digit. Confused? OK - lets try again.
We have a number: 234
we can think of it as 200 + 30 + 4 because "2" has 2 digits to its right so be multiply it by the base to the power 2. The base is ten, so 2 gest multiplied by 10^2, or 10 * 10, or 100. Similarly, the "3" has one digit to it's right, so it's multiplied by 10^1, or 10. "4" has zero, so it's multiplied by 10^0, or 1.
OK now?
We don't have to use ten as the base - if we had two thumbs instead of one, we'd probably use 12! But twelve isn;t a lot of help to computers which - as you know - only ever thing in zeros and ones. Which is actually base two, which we call "binary" and it works exactly the same as base ten, but with less digits available, and much, much longer numbers! For example, to represent "234" in binary would be "11101010" - try it for yourself using the system above:
1 * 2^7 == 1 * 128 == 128
1 * 2^6 == 1 *  64 ==  64
1 * 2^5 == 1 *  32 ==  32
0 * 2^4 == 0 *  16 ==   0
1 * 2^3 == 1 *   8 ==   8
0 * 2^2 == 0 *   4 ==   0
1 * 2^1 == 1 *   2 ==   2
0 * 2^0 == 0 *   1 ==   0
                      ---
                      234
                      ---

A common base in "computer speak" is base 16 - or hex because it's easy to work into binary for the computer, and also a lot shorter than binary!
For hex, we need sixteen digits: 0 to 9, plus six others, for which we use the alphabet: A, B, C, D, E, and F.
So 234 in Hex is "EA":
E == 14 in base ten * 16^1 == 14 * 16 == 224
A == 10 in base ten * 16^0 == 10 *  1 ==  10
                                         ---
                                         234
                                         ---

So... 128 in hex is 80:
8 * 16 ^ 1 == 8 * 16 == 128
0 * 16 ^ 0 == 0 *  1 ==   0
                        ---
                        128
                        ---

And you can work the rest out for yourself using the same principles! :laugh:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900