Hey everyone, this is a collection of FAQs from the old forums that I am copying over here for easier access.
Hope they help,
The Codecademy Moderators.
1st problem - Quotes on a Variable
Once a word has been declared as a variable
it becomes a reserved word.
If you wrap it in quotes it loses the variable power because it becomes just a string of characters.
Examples:
var myCountry = "USA";
console.log(myCountry.length);
console.log(myCountry.substring(0,3));
“USA” is wrapped in quotes because it is a string of characters.
myCountry should not take quotes because it is a variable.
A variable is a memory location where we can save data.
var myNumber = 333;
Although 333
is data just like "USA"
, it does not get wrapped with quotes because it is a number. Only strings of characters get wrapped in quotes.
If I write "333"
, the value becomes a string of characters, 3 being the character. The problem with this is that it can not be used in math.
See my later post for more clarification on this.
2nd Problem - if/else statements
if/else
statements have a very strict structure you must follow:
A typical if () statement looks like this. Depending on what you are checking for, the else if and else blocks may not be needed. Note how the { }, ( ) and ; are used.
if (this condition is true) {
do something;
} else if {
do this other thing;
} else {
do something different;
}
You need to make sure all your {
}
are used correctly and in the right places. Before writing else if
you need to have closed your if
statement with a }
.
Also notice where the ;
are used. They are NOT used after the condition is declared like this if (condition) ;
that will cause your program to break. ;
should only be used in the statements within the if
block as they signify the end of an instruction.
Thanks to the following users, who were brilliant enough to make the original posts!
Tony de Araujo
Albions