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

When I make changes they don't APPEAR! JS

I'm trying to make it to where I can change the value of someones checking or savings account. Whenever I click the ADD CASH link I type savings or checking and then I type amount. There are no errors or anything but whenever I refresh the page the changes aren't there. Is there some way to make the changes stay after a refresh? Here's the code! I also added code for a button I made incase that would be easier for you to find a sloution to!

//This is the code for the link
var money = false;
var silver = false;
var student;

var students = [
  { 
    name: 'Luke',
    memberSince: 'October 9, 2017',
    memberShip: 'Platinum',
    OuncesofSilver: 4,
    savings: 0,
    checking: 1.60,
    debts: 0,
    booksDue: 0,
    points: 50,
    goldGrams: 0
  },
  { 
    name: 'Julie',
    memberSince: 'October 9, 2017',
   memberShip: 'Gold',
   OuncesofSilver: 0,
   savings: 0,
   checking: 0,
   debts: 0,
   booksDue: 0,
   points: 10,
   goldGrams: 0
  },
  {
    name: 'Brendon',
    memberSince: 'Nov 9, 2017',
    memberShip: 'Platinum',
    OuncesofSilver: 26,
    savings: 0,
    checking: 425,
    debts: 0,
    booksDue: 0,
    points: 0,
    goldGrams: 0
  }

];


const getReport = ( student ) => {
  let report = `<h1 class="a"> ${student.name} </h1>`;
   `<ol>`
  report += `<li class="s"> MemberShip: ${student.memberShip} </li>`;
  report += `<li class="d"> MemberSince: ${student.memberSince} </li>`;
  report += `<li class="f"> Points: ${student.points} </li>`;
  report += `<li class="g"> Checking: ${student.checking} </li>`;
  report += `<li class="h"> Debts: ${student.debts} </li>`;
  report += `<li class="j"> Savings: ${student.savings + parseInt(student.OuncesofSilver * 17.08) + parseInt(student.goldGrams * 41.15)} </li>`;
    report += `<li class="k"> Ounces of Silver: ${student.OuncesofSilver} </li>`;
    report += `<li class="l"> Books Due: ${student.booksDue} </li>`;
   report += `<li class="z"> GoldGrams: ${student.goldGrams} </li>`;
   `</ol>`
  return report;
 }

money = prompt("Where would you like to add your money?");
while(true) {
if ( money === 'savings' ) {
  silver = true;
  save = prompt(" Type the amount you want to add(savings) " );
  document.write("<h1>You added " + save + " dollars to your savings account!<h1>");
  break;
} 
  else if ( money === 'checking' ) {
    money = true;
  spend = prompt(" Type the amount you want to add(checking) ");
    document.write("<h1>You added " + spend + " dollars to your checking account!<h1>");
  break;
} 

 }

if ( silver === true ) {
students[0].savings += save;
}
if ( money === true ) {
students[0].checking += spend;
}

//This is the code for button(I only need a solution to one of these)

var money = false;
var silver = false;
var student;

var students = [
  { 
    name: 'Luke',
    memberSince: 'October 9, 2017',
    memberShip: 'Platinum',
    OuncesofSilver: 4,
    savings: 0,
    checking: 1.60,
    debts: 0,
    booksDue: 0,
    points: 50,
    goldGrams: 0
  },
  { 
    name: 'Julie',
    memberSince: 'October 9, 2017',
   memberShip: 'Gold',
   OuncesofSilver: 0,
   savings: 0,
   checking: 0,
   debts: 0,
   booksDue: 0,
   points: 10,
   goldGrams: 0
  },
  {
    name: 'Brendon',
    memberSince: 'Nov 9, 2017',
    memberShip: 'Platinum',
    OuncesofSilver: 26,
    savings: 0,
    checking: 425,
    debts: 0,
    booksDue: 0,
    points: 0,
    goldGrams: 0
  }

];


const getReport = ( student ) => {
  let report = `<h1 class="a"> ${student.name} </h1>`;
   `<ol>`
  report += `<li class="s"> MemberShip: ${student.memberShip} </li>`;
  report += `<li class="d"> MemberSince: ${student.memberSince} </li>`;
  report += `<li class="f"> Points: ${student.points} </li>`;
  report += `<li class="g"> Checking: ${student.checking} </li>`;
  report += `<li class="h"> Debts: ${student.debts} </li>`;
  report += `<li class="j"> Savings: ${student.savings + parseInt(student.OuncesofSilver * 17.08) + parseInt(student.goldGrams * 41.15)} </li>`;
    report += `<li class="k"> Ounces of Silver: ${student.OuncesofSilver} </li>`;
    report += `<li class="l"> Books Due: ${student.booksDue} </li>`;
   report += `<li class="z"> GoldGrams: ${student.goldGrams} </li>`;
   `</ol>`
  return report;
 }

money = prompt("Where would you like to add your money?");
while(true) {
if ( money === 'savings' ) {
  silver = true;
  save = prompt(" Type the amount you want to add(savings) " );
  document.write("<h1>You added " + save + " dollars to your savings account!<h1>");
  break;
} 
  else if ( money === 'checking' ) {
    money = true;
  spend = prompt(" Type the amount you want to add(checking) ");
    document.write("<h1>You added " + spend + " dollars to your checking account!<h1>");
  break;
} 

 }

if ( silver === true ) {
students[0].savings += save;
}
if ( money === true ) {
students[0].checking += spend;
}

1 Answer

Steven Parker
Steven Parker
243,199 Points

Normally anything you enter on a page will not remain after a refresh. To do that, you might use a form to submit the data back to the server, which would store it in a database and then use it to modify the page when the refresh request was received. Or you might use local storage in the browser to store and replace the data without the server's involvement.

For practical purposes where a checking/savings account would be handled, a database combined with server coding would almost certainly be part of the solution.

Ok, it's no big deal just would've been cool, thanks for the help as always!

Steven Parker
Steven Parker
243,199 Points

There's a short workshop on local storage which might be useful if you only want to make a demo that would persist data across refreshes.

Great! I'll check it out!