Javascript help [SOLVED]

lightwriter
16 Jun 2015, 18:44
I'm trying to make a game using a real time clock... I got the actual clock display to work but I'm having issues with the store hours part. Nothing is happening. I'm not sure if the issue is with Javascript or in Quest.
Javascript (I originally had 2 functions):
setInterval (function () {furniturestorehours()}, 1000)

function grocerystorehours() {
var grocerystoreopen;
variable currentdate = new Date();
var furniturehours = currentdate.getHours();
if ((furniturehours >= 8) && (furniturehours <= 20)) {
var furniturestoreopen = true;
}
else {
var furniturestoreopen = false;
}
ASLEvent ("furniturestorehours", furniturestoreopen);
var grocerystoreopen;
variable currentdate2 = new Date();
var groceryhours = currentdate2.getHours();
if ((groceryhours >= 7) && (groceryhours <= 21)) {
var grocerystoreopen = true;
}
else {
var grocerystoreopen = false;
}
ASLEvent ("grocerystorehours", grocerystoreopen);
}

Here is my test code in Quest: (I have tried different variations
game.grocerystore = grocerystoreopen
if (game.grocerystore == true) {
msg ("It works!")
}

lightwriter
16 Jun 2015, 20:28
I spotted a few typos in my Javascript code:
here is a simpler version:
setInterval(function () {storehours()}, 1000;
function storehours() {
var furniturestoreopen;
var currentdate2 = new Date();
if (currentdate2.getHours() >= 8 && currentdate2.getHours() <= 20) {
var furniturestoreopen = "open";
}
else {
var furniturestoreopen = "close";
}
ASLEvent ("furniturestorehours", furniturestoreopen);
}

jaynabonne
16 Jun 2015, 21:32
First, a debugging tip: while running your game in the desktop editor, click "HTML Tools" in the menu bar. In the window that comes up, click "Console", and you will see any JavaScript errors you have.

Now, on to the errors...

The main problem you have is that your setInterval line needs to have a paren after 1000:

setInterval(function () {storehours()}, 1000);


Second, in your if/else, you don't want the extra "var"s in front of the assignment. You want to use the variable that's already defined. (It turns out that JavaScript doesn't have block scope, so they will all resolve to the same variable anyway, but just from a conceptual point of view, it's wrong.)

Finally, a simplifying thought. If you move the setInterval below the definition for storeHours, then you can pass it directly.

Overall, then, here is an edited version.

function storehours() {
var furniturestoreopen;
var currentdate2 = new Date();
if (currentdate2.getHours() >= 8 && currentdate2.getHours() <= 20) {
furniturestoreopen = "open";
}
else {
furniturestoreopen = "close";
}
ASLEvent ("furniturestorehours", furniturestoreopen);
}

setInterval(storehours, 1000);


Edit: And just for grins, you can reduce it down to this: :)

setInterval(function() {
var hour = new Date().getHours();
ASLEvent ("furniturestorehours", (hour >= 8 && hour <= 20) ? "open" : "close");
}, 1000);

jaynabonne
16 Jun 2015, 21:36
I also assume you have a function like this:

<function name="furniturestorehours" parameters="state">
game.grocerystore = (state = "open")
if (game.grocerystore) {
msg ("It works!")
}
</function>


Edit: fixed the '==' problem.

lightwriter
17 Jun 2015, 02:39
Thanks! Here I was thinking the issue was something complicated like the changing of code or something... when all it was was a simple typo

HegemonKhan
17 Jun 2015, 05:58
hopefully, when I get done with the codecademy site on JS, I'll be able to follow~read~understand that JS code above better... but right now... I can't :( as it's structure~format~syntax is still foreign~alien~"greek" to me, sighs.