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

evannex
evannex
2,436 Points

This simple code crashes my browser completely!

Okay so its a simple bit of code that I was doing just to experiment with JavaScript. I've worked with a few different programming languages before mostly C# and Java, but a few others as well, and things similar to this have always been go-to's for figuring out syntax and how to make things work in a new language, so I was trying something similar in JavaScript and for some reason it crashes my browser.

Not in a small, page stops responding kind of way, but full on chrome freezes for 5 minutes while I wait for it to officially "stop responding" so I can kill it. (I'm on a work pc so no CTRL + ALT + DEL and no Task Manager...).

I'm probably just overlooking something simple somewhere but I just can't see it. And it doesn't help that workspaces lacks proper debugging tools, from what I've found.

So here is the JavaScript:

/*
  Assumptions:
    40 hours of work a week
    50 weeks out of the year
*/

var hourly = 15;
var expenses = 12000; //$2000 per month / 2 people = 1000 * 12 months = 12000
var splurge = 7800; // $150 splurge per week * 52 weeks in a year = 7800
var savings = 0;
var apy = 0.0075; // assuming an apy around 0.75% = 0.0075

var income;
var currYear;

function main(){
 console.log("begin calculations"); 

  for(currYear = 1; currYear < 37; currYear++){
    console.log("year: " + currYear);

    //update the values that change on certain years
    if (currYear = 2) hourly = 20;
    else if (currYear = 4) hourly = 25;
    else if (currYear = 6) hourly = 30;
    else if (currYear = 10) hourly = 40;
    else if (currYear = 15) hourly = 50;
    else if (currYear = 20) hourly = 60;
    else if (currYear = 25) hourly = 75;
    else if (currYear = 30) hourly = 100;

     //do the required calculations every year
    CalculateExpenses();
    CalculateIncome();
    CalculateSavings();

    document.write("In year " + currYear + ", you would earn $" + income +
                  ". Your expenses would be $" + expenses + 
                  ", leaving you with $" + (income - expenses) + 
                  ". After splurge funds are removed you would have $" + 
                  (income - expenses - splurge) + " to put into savings." +
                  "Your new savings total after APY would be $" + savings +
                  ".");
    document.write("<br>");
  }
}

function CalculateExpenses(){
  //Assume a slight yearly increase in expenses of 1% per year.
  expenses = expenses + (expenses * 0.01);
}

function CalculateIncome(){
  // calculate the income based on the hourly rate 
  income = hourly * 2000; // 40 hours * 50 weeks = 2000 
}

function CalculateSavings(){
  // add income to savings account
  // calculate interest on new savings account balance
  // add the interest to the savings account
}

main();

1 Answer

Christopher Debove
PLUS
Christopher Debove
Courses Plus Student 18,373 Points

In your series of conditions to currYear. Your reaffecting the value of currYear (which ends in an infinite loop).

What you want to do is compare, so you need to use the comparison operator "==", like this :

    if (currYear == 2) hourly = 20;
    else if (currYear == 4) hourly = 25;
    else if (currYear == 6) hourly = 30;
    else if (currYear == 10) hourly = 40;
    else if (currYear == 15) hourly = 50;
    else if (currYear == 20) hourly = 60;
    else if (currYear == 25) hourly = 75;
    else if (currYear == 30) hourly = 100;
evannex
evannex
2,436 Points

Oh my! Wow I can't believe I did that! Thank you so much!