Skip to content

Instantly share code, notes, and snippets.

@raineorshine
Created July 28, 2013 18:31
Show Gist options
  • Select an option

  • Save raineorshine/6099557 to your computer and use it in GitHub Desktop.

Select an option

Save raineorshine/6099557 to your computer and use it in GitHub Desktop.
Practice with if statements
/*
Exercise #1:
1. declare a variable and assign it the value "hello"
2. write an if-then statement that prints "good" to the console if the variable contains more than 3 characters, otherwise it prints "not enough"
3. reassign the variable to have the value "boo"
4. run the if-statement again
*/
var hello= "hello"
if (hello.length >3) {
console.log ("good");
}
else { console.log ("not enough");
}
hello = "boo"
if (hello.length >3) {
console.log ("good");
}
else { console.log ("not enough");
}
/*
Exercise #2:
1. prompt the user to enter a string
2. if the first letter in the string is an 's', print out 'super!'
- you can access letters in a string, as if it was an array of characters
- e.g. var name = "Raine";
console.log(name[3]); // would print out 'n'
3. if the first lettler of the string is an 's' and the string is longer than 5 characters, print out 'superific!'
4. otherwise print out "that's nice"
single equals (=) : assignment (storing a value in a variable)
triple equals (===) : equivalence/comparison (i.e. testing if A is equal to B)
double equals (==) : loose equivalence (don't use!)
and (&&)
or (||)
// MULTIPLE CASES
if(A) {
doSomething();
}
else if(B) {
doSomething();
}
else if(C) {
doSomething();
}
else {
doSomethingElse();
}
What's the difference between the above and below?
if(A) {
doSomething();
}
if(B) {
doSomething();
}
if(C) {
doSomething();
}
else {
doSomethingElse();
}
*/
var name = prompt ("What's your name?");
if (name[0]==="s") {
console.log ("super!");
}
if (name[0]=== "s" && name.length>5){
console.log("superific!");
}
else {
console.log ("that's nice");
}
var name = prompt ("What's your name?");
if (name[0]==="s"&& name.length>5) {
console.log ("superific!");
}
else if (name[0]=== "s" ){
console.log("super!");
}
else {
console.log ("that's nice");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment