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 trialbrendon hanson
6,191 PointsNeed help with JS!
The security variable is equal to whatever I type in prompt. WHen I console.log it seems like it updates fine. However at the end of the code my 3 if statements dont work. ANy thoughts?
let message = '';
let student;
let question;
var i;
let security;
let input;
let returned = false;
let late = false;
const odds = document.querySelectorAll('li:nth-child(odd)');
const evens = document.querySelectorAll('li:nth-child(even)');
const libraryAccess = document.getElementById('library');
const user = document.getElementById('user');
function signIn() {
return security = prompt("Type name to continue");
}
user.addEventListener('click', signIn);
for (let i = 0; i < odds.length; i += 1) {
odds[i].style.backgroundColor = 'red';
}
for (let i = 0; i < evens.length; i += 1) {
evens[i].style.backgroundColor = 'lightgreen';
}
//Custom Function that replaces document.write with a better alternative
const print = (message) => {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
// These are the categories
const getReport = (student) => {
let report = `<h1 class="titleName" id="name"> ${student.name} </h1>`;
`<ol class="lists">`
report += `<li> MemberShip: ${student.memberShip} </li>`;
report += `<li> MemberSince: ${student.memberSince} </li>`;
report += `<li> Points: ${student.points} </li>`;
report += `<li> Checking: $${student.checking} </li>`;
report += `<li> Debts: ${student.debts} </li>`;
report += `<li> Savings: $${student.savings + parseFloat(student.OuncesofSilver * 17.51) + parseFloat(student.goldGrams * 43.47)} </li>`;
report += `<li> Ounces of Silver: ${student.OuncesofSilver} </li>`;
report += `<li> Books Due: ${student.booksDue} </li>`;
report += `<li> GoldGrams: ${student.goldGrams} </li>`;
`</ol>`
return report;
}
// Alerts for when something goes above or below certain criteria
const notify = (urgent) => {
if (returned === true) {
students[i].points += 10;
}
if (late === true) {
students[i].points -= 10;
}
//if (payedOnTime === true) {
// students[i].points += 10;
//}
if (students[i].checking > 20) {
students[i].points += 1;
}
if (students[i].points > 250) {
alert(" 1 Month Free for any membership! OR wait for a bigger Reward!");
}
if (students[i].points > 500) {
alert("50% of membership for 5 whole MONTHS! OR wait for better prize!");
}
if (students[i].points > 1000) {
alert("Upgrade to next membership!");
}
if (students[i].checking < 20) {
alert("Your checking account is low on cash! Get it to 20 dollars or you'll lose points soon!");
}
if (students[i].debts > 0) {
alert("You owe money pay it off ASAP!");
}
if (students[i].booksDue > 0) {
alert("You have book(s) due!");
}
if (students[i].savings < 1 && students[i].OuncesofSilver < 1 && students[i].goldGrams < 1) {
alert("You need to start saving!");
}
library.addEventListener('click', () => {
if (students[i].booksDue === 0) {
window.location = "library.html";
} else {
alert("You can not use this until you return your book!");
}
});
}
// security to only allow person with right password to log into their specific account
if (security === 'luke') {
var i = 0;
notify(student);
message = getReport(students[0]);
print(message);
}
if (security === 'julie') {
var i = 1;
notify(student);
message = getReport(students[1]);
print(message);
}
if (security === 'brendon') {
var i = 2;
notify(student);
message = getReport(students[2]);
print(message);
}
print(message);
print(message);
1 Answer
Steven Parker
231,846 PointsIt's a timing issue. The "if" statements run immediately when the code is loaded, but there is nothing in the "security" variable until after the user answers the prompt question.
brendon hanson
6,191 Pointsbrendon hanson
6,191 PointsI put the if statements in a function(secure) and then added the function after the other function on the event listener. No errors but the report still isn't showing on the page.
Steven Parker
231,846 PointsSteven Parker
231,846 PointsI don't think "addEventListner" can work with more than one listener argument.
But you might try calling the "secure" function from inside the "signIn" function.
brendon hanson
6,191 Pointsbrendon hanson
6,191 PointsThat worked! Thanks for the help