treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Code Academy help...

  • Hi guys and girls

    I am just working my way through a Javascript section on Code Academy and I'm really liking it up to now, however I'm stuck on this question;

    The President has notified you that he only wants you to square the ODD numbers between 100 and 150.

    Add an if...else statement to your for loop block that looks at each number. If it is odd, print the square of the number. Otherwise, just print the number.

    I had this but I think I'm way off;

    for (var i = 100; i <= 150; i++) {
      var square = i % i;
    
      if (square) {
        print (square);
      }
      else {
        print (square)
      }
    }
    
  • I think you want to do

    if(i % 2) {
        print i*i;
    }
    

    It checks to see if there's a remainder from i divided by 2. If there is then i is odd. If it is odd then square it.

  • @tbwiii

    Thanks so much :) I had to amend it a tiny bit but you were right;

    for (var i = 100; i <= 150; i++) {
    
      if (i % 2) {
        print (i * i);
      }
      else {
        print (i);
      }
    
    } //End of for statement
    
  • Sorry! I was on my phone at the time or I would have typed the whole solution out :)

  • It's okay :) you gave me what I needed really. Helped me get more used to the syntax adding the brackets etc. Really liking this JavaScript atm