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

Code Challenge: Numbers 2

Task: There is a variable "ageIfILiveToYear2100" already defined but it has a lot of decimal places. Create a variable named "age" and use the Math.floor method to lop off the decimal digits.

When I try my code: var age = Math.floor(var ageIfILiveToYear2100); I get a notification saying that my previous task is no longer passing.

Maybe Giovanni CELESTE can help with this one too. Code is below.

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>JavaScript Foundations: The Math Object</title>
    <link rel="stylesheet" href="core.css">
    <script>
      var diameter = 5.75,
          a = 17,
          b = 42,
          c = 1337,
          ageIfILiveToYear2100 = (new Date(2100, 0, 1) - new Date(1995, 5, 16, 7, 20)) / (1000 * 60 * 60 * 24 * 365.242);
      var circumference = Math.PI * diameter;
      var chance = Math.random() * 20;
      var maxWidth = Math.max(a, b, c);
      /*
      The JavaScript Math object has the following
      constant (among others):
      Math.PI
          Approximately 3.14159. Pi (?) is used
          to determine the circumference of a circle
          given its diameter by multiplying the diameter
          by ?. Geometery refresher: the circumference
          of a circle is the distance around the outside
          edge. The diameter is the width of the circle.

      The JavaScript Math object also has the following
      methods (among others):
      Math.random()
          Returns a random number between 0 and 1
      Math.max(a, b, ...)
          Returns the largest value of the passed
          arguments
      Math.floor(a)
          Returns the value of a number rounded down to
          the nearest integer
      */



    </script>
  </head>
  <body>
    <h1>JavaScript Foundations</h1>
    <h2>The Math Object</h2>
    <script src="viewer.js"></script>
  </body>
</html>

7 Answers

Hello, Kyle.

You don't have to declare the ageIfILiveToYear2100 variable again, you just pass it as an argument to the Math.floor() function. Also, check your syntax. You could be missing a semicolon or a var declaration.

var circumference = diameter * Math.PI;
var chance = Math.random() * 20;
var maxWidth = Math.max(a, b, c);
var age = Math.floor(ageIfILiveToYear2100); // No declaration here

Hope this helps.

Hi again Kyle,

As I mentionned before :), you should give us what you wrote directly in your code (then we can copy/paste it directly, and see if it works). Also, please give attention at the title of your topic, you give it the title "Code Challenge: number 2", which is the wrong challenge. Here we should see: Code challenge: the Math Object part 4 of 4. And if you can, give us the link of the code challenge too, it would help us tracking where you are (I had to look by myself, and went to code challenge number 2, which is not the one you are trying to do).

So, time to work:

I did the challenge, wrote the exact same code for var age. I guess it is some basic mistake like forgeting a semicolon or something like that. Could you please copy/paste the exact code you wrote to have your bummer please? Thanks :).

sorry...I included my code in my initial typing up top.

var age = Math.floor(var ageIfILiveToYear2100);

I'm sure that's wrong, but I honestly have no clue where to start and watched/re-watched the video like 6 times.

Oh OK. That makes sense. So where am I going wrong with the numbers? Should I choose the whole var to use Math.floor? Still slightly baffled with what goes where.

Did you try to fix your code? What did you do?

I could give you the answer, but I think it's better to understand what you do wrong and how to correct it. And understand deep the logic behind it.

Tell me what you understood from what I stated, and how you would apply to your code? Simply told, write your new (guessed) code, and if you can, explain what you did (or tried to do).

In my opinion, it's a good practice to have an experienced friend or contact to exchange (or the forum) for discussing. Exchanging about what you understand and don't understand, and the experienced one would detect if your knowledge is a bit off (and well, you can't know deeply something without experiences of what you do right or wrong).

Giovanni CELESTE and Victor Arellano , Thanks!! Victor, I saw my problem was that I didn't have the semicolon and had var included in the parentheses. Many thanks to both of you!

I saw it the first time, but what I am saying is you should include it within the entire code (here I have to copy/paste 2 times when I could copy/paste it one), well I am picky, but with a more complex code it could be helpful for others to have the block of code, and not some snippets here and here, you could have place it the wrong place, or mispell it in your code but not in your text, etc... (easier to debug with the entire problem, and then isolate).

Well, back to the code: Your mistake is that you overwrite your variable in your call. If you do something like this

var name = Giovanni;

And then call the variable, you write if like this callFunction(name);

If you write it callFunction(var name); name equals 0 (because you overwrite the previous name with a new one, empty).