Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Foundations Numbers Creating Numbers: Part 2

Declare variables with Octal numbers

<script> var mevol = 027; </script>

My assumption is the 027 is an Octal number of 16 and given a variable name mevol. In the challenge it is stating my above code is incorrect because <code>var 0 = 021, </code>. the code is embedded with <script> </script>, is this the wrong tag?

You want to use a backtick for embedding code.

See this post for more tips: https://teamtreehouse.com/forum/how-to-type-code-in-the-forum

3 Answers

You're correct in that you can only go up to 7. The calculation though involves multiplication and addition. The octal system is based on powers of 8 much like our decimal system is based on powers of 10.

In decimal we have the 1's position, 10's position, 100's position, 1000's position, ... All powers of 10 - 10^0, 10^1, 10^2, 10^3, ... In octal we have the 1's position, 8's position, 64's position, 512's position, ... All powers of 8 - 8^0, 8^1, 8^2, 8^3, ...

So to convert that octal number properly it might help to see how a decimal number would be written out in expanded form using the powers of 10.

(I'm bolding the digits of the number to help set them apart) 348 = 3 x 100 + 4 x 10 + 8 x 1

We can convert octal 027 to decimal using the same idea but using the powers of 8 instead of 10. 027 = 2 x 8 + 7 x 1 = 23 decimal

Here's a larger octal number to get a better idea: 0347 = 3 x 64 + 4 x 8 + 7 x 1 = 231 decimal

In the challenge they wanted you to convert decimal 16 to octal. I don't think they gave you a mathematical way to go from decimal to octal. Since it's a small number I think that they wanted you to figure it out based on a little trial and error.

Given 027 octal = 23 decimal you might realize that you could subtract 7 from each and arrive at 020 octal = 16 decimal

Here's a link for a mathematical way to convert a decimal number to octal if you're interested: http://www.wikihow.com/Convert-from-Decimal-to-Octal
You could start with decimal 16 and see if you come up with octal 020.

Does this help or did i make it more confusing?

Another thing that might help you understand octal better is learning how to count in octal.

Are you familiar with how old car odometers worked with the dials? The rightmost dial would rotate from 0 to 9 and then it would come back to 0 while the next one to the left would increase by 1. In counting this would be 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10...

You can count in octal this way if you imagine the dials only read from 0 to 7. The rightmost dial would rotate from 0 to 7 and then it would come back to 0 while the next one to the left would increase by 1. In counting this would be 0, 1, 2, 3, 4, 5, 6, 7, 10...

Since the challenge number was a small decimal number (16) you could arrive at the answer by simply counting decimal and octal alongside each other until you reach decimal 16. I would not suggest doing this for big numbers of course.

decimal - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 octal - 00, 01, 02, 03, 04, 05, 06, 07, 010, 011, 012, 013, 014, 015, 016, 017, 020

  • think of how the dials would turn on an octal based odometer when going from 017 to 020 if it doesn't make sense.

So we can see from the table that decimal 16 is equal to octal 020

Another thing that might help you understand octal better is learning how to count in octal.

Are you familiar with how old car odometers worked with the dials? The rightmost dial would rotate from 0 to 9 and then it would come back to 0 while the next one to the left would increase by 1. In counting this would be 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10...

You can count in octal this way if you imagine the dials only read from 0 to 7. The rightmost dial would rotate from 0 to 7 and then it would come back to 0 while the next one to the left would increase by 1. In counting this would be 0, 1, 2, 3, 4, 5, 6, 7, 10...

Since the challenge number was a small decimal number (16) you could arrive at the answer by simply counting decimal and octal alongside each other until you reach decimal 16. I would not suggest doing this for big numbers of course.

decimal - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
octal - 00, 01, 02, 03, 04, 05, 06, 07, 010, 011, 012, 013, 014, 015, 016, 017, 020

  • think of how the dials would turn on an octal based odometer when going from 017 to 020 if it doesn't make sense.

So we can see from the table that decimal 16 is equal to octal 020

Sorry for the double posted comment. I was trying to fix a formatting error.

Editing comments seems to be a little broken. Your comment disappears and then it double posts when you put your comment back in.

027 octal = 23 base 10

Let me know if you're not sure why it's 23

Got It! thank you..very appreciative. that's cleared up now, so i can move on :)

Its the octal that is incorrect. I calculated 027=16 because 7+2+7 = 16; 0-7 is the octal limit than start again with a new number such as 2 and so on. I really misunderstood this in the videos and even look up on research. Could you provide me a link that explains octal better, so i can correct this. thank you so much...