"John"
"John".length
3+7
100*100
eggplant
// This is a comment that the computer will ignore.
// It is for your eyes only!
"cake".length
confirm('This is an example of using JS to create some interaction on a website. Click OK to continue!');
// Also try the Q&A forum to get help
// The link is on the bottom left of the page!
confirm("Hello, world!")
prompt("What is your name?")
"Codecademy is really cool!".length
"I'm coding like a champ!".length > 10
console.log(2*5)
console.log("Hello")
// Here is an example of using the greater than (>) operator.
console.log(15 > 4); // 15 > 4 evaluates to true, so true is printed.
// Fill in with >, <, === so that the following print out true:
console.log("Xiao Hui".length < 122);
console.log("Goody Donaldson".length > 8);
console.log(8*2 === 16);
if(prompt("What is your name?".length < 4)) {
console.log("Your name has less characters than mine :P");
}
if (prompt("What road do you want to go down?") === "One")
{
console.log("Let's go down the first road!");
}
else
{
console.log("Let's go down the second road!");
}
if(prompt("Is Codecademy the best?") === "No")
{
console.log("The condition is true");
}
else
{
console.log("The condition is false");
}
if (10 === 10)
{
console.log("You got a true!");
}
else
{
console.log("You got a false!");
}
if (12 / 4 === "Ari".length) {
confirm("Will this run the first block?");
} else {
confirm("Or the second block?");
}
if("Jon".length * 2 / (2+1) === 2)
{
console.log("The answer makes sense!");
} else {
console.log("Error Error Error")
}
console.log(14%3)
console.log(99%8)
console.log(11%3)
if(5===5) {
console.log("The first number is even");
} else {
console.log("The first number is odd");
} 10%5===0
"wonderful day".substring(3,7)
console.log("January".substring(0,3));
console.log("Melbourne is great".substring(0,12));
console.log("Hamburgers".substring(3,10));
var myAge = 100;
console.log(myAge);
// Declare a variable on line 3 called
// myCountry and give it a string value.
var myCountry = "United States of America";
// Use console.log to print out the length of the variable myCountry.
console.log(myCountry.length);
// Use console.log to print out the first three letters of myCountry.
console.log(myCountry.substring(0,3));
// On line 2, declare a variable myName and give it your name.
var myName = "John";
// On line 4, use console.log to print out the myName variable.
console.log(myName);
// On line 7, change the value of myName to be just the first 2
// letters of your name.
myName = "Jo";
// On line 9, use console.log to print out the myName variable.
console.log(myName);
var myColor = "Blue";
console.log(myColor.length);
if(5===5)
{
console.log("I finished my first course!");
}
else
{
console.log("Impossible!");
}
confirm("Ready player one?");
confirm("Ready player one?");
var age = prompt("What's your age?");
if(age < 13)
{
console.log("You may play, however we assume no responsibilty for the content of our game.")
}
else
{
console.log("Play on peasant!");
}
confirm("Ready player one?");
var age = prompt("What's your age?");
if(age < 13)
{
console.log("You may play, however we assume no responsibilty for the content of our game.")
}
else
{
console.log("Play on peasant!");
}
console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'")
confirm("Ready player one?");
var age = prompt("What's your age?");
if(age < 13)
{
console.log("You may play, however we assume no responsibilty for the content of our game.");
}
else
{
console.log("Game on!");
}
console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");
console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");
userAnswer = prompt("Do you want to race Bieber on stage?");
confirm("Ready player one?");
var age = prompt("What's your age?");
if(age < 13)
{
console.log("You may play, however we assume no responsibilty for the content of our game.");
}
else
{
console.log("Game on!");
}
console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");
console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");
userAnswer = prompt("Do you want to race Bieber on stage?");
if(userAnswer === "Yes")
{
console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!")
}
else
{
console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'")
}
confirm("Ready player one?");
var age = prompt("What's your age?");
if(age < 13)
{
console.log("You may play, however we assume no responsibilty for the content of our game.");
}
else
{
console.log("Game on!");
}
console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");
console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");
var userAnswer = prompt("Do you want to race Bieber on stage?");
if(userAnswer === "Yes")
{
console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!")
}
else
{
console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'")
}
var feedback = prompt("Please rate my game, one to ten, below!");
if(feedback > 8)
{
console.log("Thank you! We should race at the next concert!");
}
else
{
console.log("I'll keep practicing coding and racing.")
}
confirm("Ready player one?");
var age = prompt("What's your age?");
if(age < 13)
{
console.log("You may play, however we assume no responsibilty for the content of our game.");
}
else
{
console.log("Game on!");
}
console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");
console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");
var userAnswer = prompt("Do you want to race Bieber on stage?");
if(userAnswer === "Yes")
{
console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!")
}
else
{
console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'")
}
var feedback = prompt("Please rate my game, one to ten, below!");
if(feedback > 8)
{
console.log("Thank you! We should race at the next concert!");
}
else
{
console.log("I'll keep practicing coding and racing.")
}
var speed = 65;
if (speed > 80)
{
console.log("Slow down")
}
else
{
console.log("Drive safe")
}
// This is what a function looks like:
var divideByThree = function (number) {
var val = number / 3;
console.log(val);
};
// On line 12, we call the function by name
// Here, it is called 'dividebythree'
// We tell the computer what the number input is (i.e. 6)
// The computer then runs the code inside the function!
divideByThree(8);
// Below is the greeting function!
// See line 7
// We can join strings together using the plus sign (+)
// See the hint for more details about how this works.
var greeting = function (name) {
console.log("Great to see you," + " " + name);
};
greeting("John");
var foodDemand = function(food) {
console.log("I want to eat "+ food);
}
foodDemand("Pizza");
// Nicely written function:
var calculate = function (number)
{
var val = number * 10;
console.log(val);
};
var greeting = function(name)
{
console.log(name)
};
greeting("John");
var orangeCost = function(cost)
{
var costOrange = cost*5;
console.log(costOrange);
};
orangeCost(5)
// Parameter is a number, and we do math with that parameter
var timesTwo = function(number) {
return number * 2;
};
var newNumber = timesTwo(10000);
console.log(newNumber);
var quarter = function(number)
{
return number/4;
}
if (quarter(12) % 3 === 0 ) {
console.log("The statement is true");
} else {
console.log("The statement is false");
}
var perimeterBox = function(length, width)
{
return length*2+width*2;
}
perimeterBox(5,5)
var my_number = 7; //this has global scope
var timesTwo = function(number) {
var my_number = number * 2;
console.log("Inside the function my_number is: ");
console.log(my_number);
};
timesTwo(7);
console.log("Outside the function my_number is: ")
console.log(my_number);
var nameString = function (name) {
return "Hi, I am " + name;
};
nameString("John");
console.log(nameString);
var sleepCheck = function(numHours)
{
if(numHours >= 8)
{
return "You're getting plenty of sleep! Maybe even too much!";
}
else
{
return "Get some more shut eye!";
}
}
sleepCheck(10);
sleepCheck(5);
sleepCheck(8);
//No code necessary
//No code necessary
var userChoice = prompt("Do you choose rock, paper or scissors?");
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log(computerChoice);
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
//ComputerChoice Random Number to Selection Conversion Statements
if(computerChoice <= 0.33)
{
computerChoice = "rock";
}
else if(computerChoice <= 0.66)
{
computerChoice = "paper";
}
else
{
computerChoice = "scissors";
}
/*var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);*/
var compare = function(choice1, choice2)
{
if(choice1 === choice2)
{
return "The result is a tie!";
}
}
/*var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);*/
var compare = function(choice1, choice2)
{
if(choice1 === choice2)
{
return "The result is a tie!";
}
else if(choice1 === "rock")
{
if(choice2 === "scissors")
{
return "rock wins";
}
else
{
return "paper wins";
}
}
}
/*var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);*/
var compare = function(choice1, choice2)
{
if(choice1 === choice2)
{
return "The result is a tie!";
}
else if(choice1 === "rock")
{
if(choice2 === "scissors")
{
return "rock wins";
} else
{
return "paper wins";
}
} else if(choice1 === "paper") {
if(choice2 === "rock") {
return "paper wins";
} else {
return "scissors wins";
}
}
}
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
compare(userChoice, computerChoice);
var compare = function(choice1, choice2)
{
if(choice1 === choice2)
{
return "The result is a tie!";
}
else if(choice1 === "rock")
{
if(choice2 === "scissors")
{
return "rock wins";
}
else
{
return "paper wins";
}
}
else if(choice1 === "paper")
{
if(choice2 === "rock")
{
return "paper wins";
}
else
{
return "scissors wins";
}
}
}
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
compare(userChoice, computerChoice);
var compare = function(choice1, choice2)
{
if(choice1 === choice2)
{
return "The result is a tie!";
}
else if(choice1 === "rock")
{
if(choice2 === "scissors")
{
return "rock wins";
}
else
{
return "paper wins";
}
}
else if(choice1 === "paper")
{
if(choice2 === "rock")
{
return "paper wins";
}
else
{
return "scissors wins";
}
}
}
console.log("1");
console.log("2");
console.log("3");
console.log("4");
console.log("5");
for (var counter = 1; counter < 11; counter++)
{
console.log(counter);
}
// Change where the for loop starts.
for (var i = 5; i < 11; i = i + 1){
console.log(i);
}
// Edit this for loop
for (var i = 4; i < 24; i = i + 1) {
console.log(i);
}
for (var i = 5; i < 51; i+=5)
{
console.log(i);
}
for (var i = 8 ; i < 120; i+=12)
{
console.log(i);
}
// Example of infinite loop. THIS WILL CRASH YOUR
// BROWSER. Don't run the code without changing it!
for (var i = 10; i >= 0; i--) {
console.log(i);
}
for(var i=100; i>0; i-=5)
{
console.log(i);
}
var junk = ["Bunk","Trunk",4,6];
console.log(junk)
var junkData = ["Eddie Murphy", 49, "peanuts", 31];
console.log(junkData[3])
var cities = ["Melbourne", "Amman", "Helsinki", "NYC", "Boston", "Chicago"];
for (var i = 0; i < cities.length; i++) {
console.log("I would like to visit " + cities[i]);
}
var names = ["Steven","Jacob","Brent","Jack","John"];
for(var i=0; i < 5;i++)
{
console.log("I know someone called " + names[i])
}
//No code necessary
/*jshint multistr:true */
text = "Blah blah blah blah blah blah Eric \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eric";
var myName = "Eric";
var hits = [];
// Look for "E" in the text
for(var i = 0; i < text.length; i++) {
if (text[i] === "E") {
// If we find it, add characters up to
// the length of my name to the array
for(var j = i; j < (myName.length + i); j++) {
hits.push(text[j]);
}
}
}
if (hits.length === 0) {
console.log("Your name wasn't found!");
} else {
console.log(hits);
}
/*jshint multistr:true */
var text = "Lorem ipsum dolor sit amet, John consectetur adipiscing elit. Praesent quis dictum nulla. Nullam condimentum lacinia turpis a semper.";
var myName = "John";
var hits = [];
/*jshint multistr:true */
var text = "Lorem ipsum dolor sit amet, John consectetur adipiscing elit. Praesent quis dictum nulla. Nullam condimentum lacinia turpis a semper.";
var myName = "John";
var hits = [];
for(var i =0;i <= text.length; i++)
{
}
/*jshint multistr:true */
var text = "Lorem ipsum dolor sit amet, John consectetur adipiscing elit. Praesent quis dictum nulla. Nullam condimentum lacinia turpis a semper.";
var myName = "John";
var hits = [];
for(var i =0;i <= text.length; i++)
{
if(text[i] === "J")
{
}
}
/*jshint multistr:true */
var text = "Stewart containing some text for Stewart written and developed by Stewart";
var myName = "Stewart";
var hits = [];
/* assign the value of 0 to i and continue to the loop while i is less than the length of the variable 'text', and add 1 to i after executing the code block */
for (i = 0; i < text.length + 1; i++) {
/* if the array value of the 'text' variable is equal to the 0 point of the myName variable */
if(text[i] === myName[0]) {
/* then assign the value of i to j and continue the loop while j is less than the length of the myName variable plus the value of i */
for(var j = i; j < (myName.length + i); j++) {
/* if the array value of the variable 'text' is equal to the array value of j minus i for the 'myName' variable, push the value of j on to the hits array */
if(text[j] === myName[j-i]) {
hits.push(text[j]);
}
}
}
}
if(hits.length === 0){
console.log("Your name wasn't found!");
}
else {
console.log(hits);
}
/*jshint multistr:true */
var text = "Stewart containing some text for Stewart written and developed by Stewart";
var myName = "Stewart";
var hits = [];
/* assign the value of 0 to i and continue to the loop while i is less than the length of the variable 'text', and add 1 to i after executing the code block */
for (i = 0; i < text.length + 1; i++) {
/* if the array value of the 'text' variable is equal to the 0 point of the myName variable */
if(text[i] === myName[0]) {
/* then assign the value of i to j and continue the loop while j is less than the length of the myName variable plus the value of i */
for(var j = i; j < (myName.length + i); j++) {
/* if the array value of the variable 'text' is equal to the array value of j minus i for the 'myName' variable, push the value of j on to the hits array */
if(text[j] === myName[j-i]) {
hits.push(text[j]);
}
}
}
}
if(hits.length === 0){
console.log("Your name wasn't found!");
}
else {
console.log(hits);
}
/*jshint multistr:true */
var text = "Stewart containing some text for Stewart written and developed by Stewart";
var myName = "Stewart";
var hits = [];
/* assign the value of 0 to i and continue to the loop while i is less than the length of the variable 'text', and add 1 to i after executing the code block */
for (i = 0; i < text.length + 1; i++) {
/* if the array value of the 'text' variable is equal to the 0 point of the myName variable */
if(text[i] === myName[0]) {
/* then assign the value of i to j and continue the loop while j is less than the length of the myName variable plus the value of i */
for(var j = i; j < (myName.length + i); j++) {
/* if the array value of the variable 'text' is equal to the array value of j minus i for the 'myName' variable, push the value of j on to the hits array */
if(text[j] === myName[j-i]) {
hits.push(text[j]);
}
}
}
}
if(hits.length === 0){
console.log("Your name wasn't found!");
}
else {
console.log(hits);
}
var coinFace = Math.floor(Math.random() * 2);
while(coinFace === 0){
console.log("Heads! Flipping again...");
var coinFace = Math.floor(Math.random() * 2);
}
console.log("Tails! Done flipping.");
var understand = true;
while(understand === true){
console.log("I'm learning while loops!");
understand = false;
}
understand = true;
while(understand){
console.log("I'm learning while loops!");
understand = false;
}
var bool = true;
while(bool){
console.log("Less is more!");
bool = false;
}
var loop = function(){
var count = 0;
while(count < 3){
console.log("I'm looping!");
count++
}
};
loop();
var swag = true;
var soloLoop = function(){
while(swag === true) {
console.log("Looped once!");
swag = false;
}
};
soloLoop();
//No code necessary
var count = 1;
while(count < 10) {
count++;
console.log("This is a for loop and the count is...");
console.log("The count is " + count);
}
for(;count < 10;) {
count++;
console.log("This is a for loop and the count is...");
console.log("The count is " + count);
}
var loopCondition = false;
do {
console.log("I'm gonna stop looping 'cause my condition is " + loopCondition + "!");
} while (loopCondition);
var count = 1;
var getToDaChoppa = function(){
do{
console.log("Welcome to 'Nam");
count++;
} while(count<2);
};
getToDaChoppa();
for(var count = 1; count<2;count++) {
console.log("Hello, world!")
}
do{
console.log("Hello, world!")
count++;
} while(count<2);
var slaying = true;
// A bit of new math magic to calculate the odds
// of hitting the dragon. We'll cover this soon!
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
var totalDamage = 0;
while (slaying) {
if (youHit) {
console.log("You hit the dragon and did " + damageThisRound + " damage!");
totalDamage += damageThisRound;
if (totalDamage >= 4) {
console.log("You did it! You slew the dragon!");
slaying = false;
} else {
youHit = Math.floor(Math.random() * 2);
}
} else {
console.log("The dragon burninates you! You're toast.");
slaying = false;
}
}
var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
var totalDamage = 0;
var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
var totalDamage = 0;
while(slaying) {
slaying = false;
}
var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
var totalDamage = 0;
while(slaying) {
if(youHit === 1) {
console.log("You have hit the Dragon!")
}
else {
console.log("The Dragon has slain you!")
}
slaying = false;
}
var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
var totalDamage = 0;
while(slaying) {
if(youHit === 1) {
console.log("You have hit the Dragon!")
totalDamage += damageThisRound;
if(totalDamage >= 4) {
console.log("You have slain the Dragon!");
}
}
else {
console.log("The Dragon has slain you!")
}
slaying = false;
}
var slaying = true;
var youHit = Math.floor(Math.random() * 10);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
var totalDamage = 4;
while(slaying) {
if(youHit === 1) {
console.log("You have hit the Dragon!")
totalDamage += damageThisRound;
if(totalDamage >= 4) {
console.log("You have slain the Dragon! You have won!");
slaying = false;
}
else {
youHit = Math.floor(Math.random() * 2);
}
}
else {
console.log("The Dragon has slain you!")
}
slaying = false;
}
var isEven = function(number) {
if(number % 2 === 0) {
return true;
}
else {
return false;
}
};
var isEven = function(number) {
if(number % 2 === 0) {
return true;
}
else if(isNaN(number) === true) {
return "That is not a number, you must input a number!";
}
else {
return false;
}
};
for(var counter = 1;counter <= 5;counter++) {
console.log("This is a for loop");
}
var lunch = prompt("What do you want for lunch?","Type your lunch choice here");
switch(lunch){
case 'sandwich':
console.log("Sure thing! One sandwich, coming up.");
break;
case 'soup':
console.log("Got it! Tomato's my favorite.");
break;
case 'salad':
console.log("Sounds good! How about a caesar salad?");
break;
case 'pie':
console.log("Pie's not a meal!");
break;
default:
console.log("Huh! I'm not sure what " + lunch + " is. How does a sandwich sound?");
}
var color = prompt("What's your favorite primary color?","Type your favorite color here");
switch(color) {
case 'red':
console.log("Red's a good color!");
break;
case 'blue':
console.log("That's my favorite color, too!");
break;
case 'yellow':
console.log("Yellow is okay...")
break;
default:
console.log("I don't think that's a primary color!");
}
var candy = prompt("What's your favorite candy?","Type your favorite candy here.");
switch(candy) {
case 'licorice':
console.log("Gross!");
break;
case 'gum':
console.log("I like gum!");
break;
case 'beets':
console.log("...is that even a candy?");
break;
default:
console.log("I don't recognize that candy D:");
break;
}
scvar answer = prompt("How are you feeling","Insert Feels Here");
switch(answer) {
case 'Happy':
console.log("Yay, I'm happy too!");
break;
case 'Sad':
console.log("Aww, too bad!");
break;
case 'Meh':
console.log("Have a smile to brighten up your day!");
break;
default:
console.log("You're a robot...");
break;
}
var answer = prompt("What color is #TheDress?");
switch(answer) {
case 'Blue and Black':
console.log("Yeah!");
break;
case 'Gold and White':
console.log("No!");
break;
case 'It does not Matter':
console.log("Whatever");
break;
default:
console.log("That's not a valid answer.");
break;
}
var answer = prompt("What color is #TheDress?");
switch(answer) {
case 'Blue and Black':
console.log("Yeah!");
break;
case 'Gold and White':
console.log("No!");
break;
case 'It does not Matter':
console.log("Whatever");
break;
default:
console.log("That's not a valid answer.");
break;
}
// Complete lines 3 and 4!
var iLoveJavaScript = true;
var iLoveLearning = true;
if(iLoveJavaScript && iLoveLearning) {
// if iLoveJavaScript AND iLoveLearning:
console.log("Awesome! Let's keep learning!");
} else if(!(iLoveJavaScript || iLoveLearning)) {
// if NOT iLoveJavaScript OR iLoveLearning:
console.log("Let's see if we can change your mind.");
} else {
console.log("You only like one but not the other? We'll work on it.");
}
var hungry = true;
var foodHere= true;
var eat = function() {
if(hungry && foodHere){
return true;
}
else{
return false;
}
};
var tired = true;
var bored = false;
var nap = function() {
if(tired || bored){
return true;
}
else{
return false;
}
}
var programming = false;
var happy = function() {
if(programming != false){
return false;
}
else{
return true;
}
}
//No code necessary
var troll = prompt("You're walking through the forest, minding your own business, and you run into a troll! Do you FIGHT him, PAY him, or RUN?").toUpperCase();
switch(troll) {
case 'FIGHT':
var strong = prompt("How courageous! Are you strong (YES or NO)?").toUpperCase();
var smart = prompt("Are you smart?").toUpperCase();
if(strong === 'YES' || smart === 'YES') {
console.log("You only need one of the two! You beat the troll--nice work!");
} else {
console.log("You're not strong OR smart? Well, if you were smarter, you probably wouldn't have tried to fight a troll. You lose!");
}
break;
case 'PAY':
var money = prompt("All right, we'll pay the troll. Do you have any money (YES or NO)?").toUpperCase();
var dollars = prompt("Is your money in Troll Dollars?").toUpperCase();
if(money === 'YES' && dollars === 'YES') {
console.log("Great! You pay the troll and continue on your merry way.");
} else {
console.log("Dang! This troll only takes Troll Dollars. You get whomped!");
}
break;
case 'RUN':
var fast = prompt("Let's book it! Are you fast (YES or NO)?").toUpperCase();
var headStart = prompt("Did you get a head start?").toUpperCase();
if(fast === 'YES' || headStart === 'YES') {
console.log("You got away--barely! You live to stroll through the forest another day.");
} else {
console.log("You're not fast and you didn't get a head start? You never had a chance! The troll eats you.");
}
break;
default:
console.log("I didn't understand your choice. Hit Run and try again, this time picking FIGHT, PAY, or RUN!");
}
var user = prompt("What is your name?");
var user = prompt("What is your name?").toUpperCase();
var user = prompt("What is your name?").toUpperCase();
switch(user){
case 'JOHN':
console.log("I've been waiting");
break;
case 'BRENT':
console.log("I've been waiting");
break;
case 'JACK':
console.log("I've been waiting");
break;
default:
console.log("I've been waiting");
break;
}
var user = prompt("What is your name?").toUpperCase();
switch(user)
{
case 'JOHN':
if(1+1==2 && 1 == 1)
{
console.log("I've been waiting for you...");
}
else
{
console.log("Oops! You broke the game!");
}
break;
case 'BRENT':
if(1-1==0 || 1 == 1)
{
console.log("I've been waiting for you...");
}
break;
case 'JACK':
console.log("I've been waiting for you...");
break;
default:
console.log("I've been waiting for you...");
break;
}
var user = prompt("What is your name?").toUpperCase();
switch(user)
{
case 'JOHN':
if(1+1==2 && 1 == 1)
{
console.log("I've been waiting for you...");
}
else
{
console.log("Oops! You broke the game!");
}
break;
case 'BRENT':
if(1-1==0 || 1 == 1)
{
console.log("I've been waiting for you...");
}
break;
case 'JACK':
console.log("I've been waiting for you...");
break;
default:
console.log("I've been waiting for you...");
break;
}
var list = ["Bread", "Milk", "Peanut Butter"];
var languages = ["HTML", "CSS", "JavaScript", "Python", "Ruby"];
console.log(languages[2]);
var languages = ["HTML", "CSS", "JavaScript", "Python", "Ruby"];
console.log(languages[2]);
console.log(languages.length);
var languages = ["HTML", "CSS", "JavaScript", "Python", "Ruby"];
for(var counter = 0; counter<5;counter++)
{
console.log(languages[counter])
}
var myArray = [010101100110,false,"Test"];
var newArray = [[1,2,3],[4,5,6],[7,8,9]];
var jagged = [[15,67,82],[56,45]];
//No code necessary
var phonebookEntry = {};
phonebookEntry.name = 'Oxnard Montalvo';
phonebookEntry.number = '(555) 555-5555';
phonebookEntry.phone = function() {
console.log('Calling ' + this.name + ' at ' + this.number + '...');
};
phonebookEntry.phone();
var me =
{
name: "John",
age: 100
}
var me = new Object();
me.name = "John";
me.age = 100;
var object1 = new Object();
var object2 = new Object();
var object3 = new Object();
object1.name = "Object One"
object2.name = "Object Two"
object3.name = "Object Three"
//No code necessary
var myArray = [15,true,"John",myObj];
var myObj = {
type: 'fancy',
disposition: 'sunny'
};
var newArray = [[15,67,81],[myObj, 17]];
var myObj =
{
name: 'John',
Relationship: 'Friend'
};
var myObject = {
name: 'Eduardo',
type: 'Most excellent',
interests: ['Sailing','Skiing','Programming']
};
var myOwnObject = new Object();
myOwnObject.name = "John G";
myOwnObject.gender = "Male";
myOwnObject.number = "555-555-555";
var friends = {};
friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ['One Microsoft Way','Redmond','WA','98052']
};
friends.steve = {
firstName: "Steve",
lastName: "Jobs",
number: "(408) 555-5555",
address: ['1 Infinite Loop','Cupertino','CA','95014']
};
var list = function(obj) {
for(var prop in obj) {
console.log(prop);
}
};
var search = function(name) {
for(var prop in friends) {
if(friends[prop].firstName === name) {
console.log(friends[prop]);
return friends[prop];
}
}
};
list(friends);
search("Steve");
var friends = new Object();
var friends = new Object();
friends.steve = {}
friends.bill = {};
var friends = new Object();
friends.steve =
{
firstName: "Steve",
lastName: "Smith",
number: "666-666-666"
};
friends.bill =
{
firstName: "Bill",
lastName: "Smith",
number: "666-666-666"
};
var friends = new Object();
friends.steve =
{
firstName: "Steve",
lastName: "Smith",
number: "666-666-666",
address: []
};
friends.bill =
{
firstName: "Bill",
lastName: "Smith",
number: "666-666-666",
address: []
};
var friends = new Object();
friends.steve =
{
firstName: "Steve",
lastName: "Smith",
number: "666-666-666",
address: []
};
friends.bill =
{
firstName: "Bill",
lastName: "Smith",
number: "666-666-666",
address: []
};
var list = function (friends)
{
for(var firstName in friends)
{
console.log(firstName);
}
}
var friends = new Object();
friends.steve =
{
firstName: "Steve",
lastName: "Smith",
number: "666-666-666",
address: []
};
friends.bill =
{
firstName: "Bill",
lastName: "Smith",
number: "666-666-666",
address: []
};
var list = function (friends)
{
for(var firstName in friends)
{
console.log(firstName);
}
}
var search = function (name)
{
for(var firstName in friends)
{
if(friends[firstName].firstName === name)
{
console.log(friends[firstName]);
return friends[firstName];
}
}
}
var friends = new Object();
friends.steve =
{
firstName: "Steve",
lastName: "Smith",
number: "666-666-666",
address: []
};
friends.bill =
{
firstName: "Bill",
lastName: "Smith",
number: "666-666-666",
address: []
};
var list = function (friends)
{
for(var firstName in friends)
{
console.log(firstName);
}
}
var search = function (name)
{
for(var firstName in friends)
{
if(friends[firstName].firstName === name)
{
console.log(friends[firstName]);
return friends[firstName];
}
}
}
(((3 * 90) === 270) || !(false && (!false)) || "bex".toUpperCase() === "BEX");
answer = true;
// Here is an array of multiples of 8. But is it correct?
var multiplesOfEight = [8,16,24,32,40,48];
// Test to see if a number from the array is NOT a true
// multiple of eight. Real multiples will return false.
var answer = multiplesOfEight[6] % 8 !== 0;
for(var counter = 1;counter<21;counter++){
if(counter%5===0&&counter%3===0){
console.log("FizzBuzz");
}
else{
if(counter%3===0){
console.log("Fizz");
}
if(counter%5===0){
console.log("Buzz");
}
if(counter%5!==0&&counter%3!==0){
console.log(counter);
}
}
}
var getReview = function (movie){
switch(movie) {
case "Toy Story 2":
return("Great story. Mean prospector.");
break;
case "Finding Nemo":
return("Cool animation, and funny turtles.");
break;
case "The Lion King":
return("Great songs.");
break;
default:
console.log("I don't know!");
}
}
console.log("I'm ready for Objects!");
var bob = new Object()
var me = {
age: 22,
country: "United States"
};
var bob = {
name: "Bob Smith",
age: 30
};
var susan = {
name: "Susan Jordan",
age: 25
};
// here we save Bob's information
var name1 = bob.name;
var age1 = bob.age;
// finish this code by saving Susan's information
var name2 = susan.name
var age2 = susan.age
// Take a look at our next example object, a dog
var dog = {
species: "greyhound",
weight: 60,
age: 4
};
var species = dog["species"];
// fill in the code to save the weight and age using bracket notation
var weight = dog["weight"];
var age = dog["age"];
var bob = new Object();
bob.name = "Bob Smith";
bob.age = 30;
var susan1 = {
name: "Susan Jordan",
age: 24
};
var susan2 = new Object();
susan2.name = "Susan Jordan";
susan2.age = 24;
var snoopy = {
species: "beagle",
age: 10
}
var buddy = new Object();
buddy.species = "golden retriever";
buddy.age = 5;
var bicycle = new Object();
bicycle.speed = 0;
bicycle.gear = 1;
bicycle.frame_material = "carbon fiber";
// Accepts a number x as input and returns its square
var square = function (x) {
return x * x;
};
// Write the function multiply below
// It should take two parameters and return the product
var multiply = function (x,y) {
return x*y;
};
multiply(15,10);
// here is bob again, with his usual properties
var bob = new Object();
bob.name = "Bob Smith";
bob.age = 30;
// this time we have added a method, setAge
bob.setAge = function (newAge){
bob.age = newAge;
};
// here we set bob's age to 40
bob.setAge(40);
// bob's feeling old. Use our method to set bob's age to 20
bob.setAge(20);
var bob = new Object();
bob.age = 17;
// this time we have added a method, setAge
bob.setAge = function (newAge){
bob.age = newAge;
};
bob.getYearOfBirth = function () {
return 2014 - bob.age;
};
console.log(bob.getYearOfBirth());
// here we define our method using "this", before we even introduce bob
var setAge = function (newAge) {
this.age = newAge;
};
// now we make bob
var bob = new Object();
bob.age = 30;
// and down here we just use the method we already made
bob.setAge = setAge;
// change bob's age to 50 here
bob.setAge(50);
// here we define our method using "this", before we even introduce bob
var setAge = function (newAge) {
this.age = newAge;
};
// now we make bob
var bob = new Object();
bob.age = 30;
bob.setAge = setAge;
// make susan here, and first give her an age of 25
var susan = new Object();
susan.age = 25;
susan.setAge = setAge;
// here, update Susan's age to 35 using the method
susan.setAge(35);
var rectangle = new Object();
rectangle.height = 3;
rectangle.width = 4;
// here is our method to set the height
rectangle.setHeight = function (newHeight) {
this.height = newHeight;
};
// help by finishing this method
rectangle.setWidth = function (newWidth){
this.width = newWidth;
}
rectangle.setWidth(8);
rectangle.setHeight(6);
// here change the width to 8 and height to 6 using our new methods
var square = new Object();
square.sideLength = 6;
square.calcPerimeter = function() {
return this.sideLength * 4;
};
square.calcArea = function() {
return Math.pow(this.sideLength, 2);
};
var p = square.calcPerimeter();
var a = square.calcArea();
// here we make bob using the Object constructor
var bob = new Object();
bob.name = "Bob Smith";
bob.age = 20;
// add bob's age here and set it equal to 20
function Person(name,age) {
this.name = name;
this.age = age;
}
// Let's make bob and susan again, using our constructor
var bob = new Person("Bob Smith", 30);
var susan = new Person("Susan Jordan", 25);
var george = new Person("George Washington", 275);
function Cat(age, color) {
this.age = age;
this.color = color;
}
// make a Dog constructor here
function Dog(age, breed, color) {
this.age = age;
this.breed = breed;
this.color = color;
}
function Person(name,age) {
this.name = name;
this.age = age;
this.species = "Homo Sapiens";
}
var sally = new Person("Sally Bowles", 39);
var holden = new Person("Holden Caulfield", 16);
console.log("sally's species is " + sally.species + " and she is " + sally.age);
console.log("holden's species is " + holden.species + " and he is " + holden.age);
function Rectangle(height, width) {
this.height = height;
this.width = width;
this.calcArea = function() {
return this.height * this.width;
};
this.calcPerimeter = function() {
return this.height*2+this.width*2;
}
}
var rex = new Rectangle(7,3);
var area = rex.calcArea();
var perimeter = rex.calcPerimeter();
function Rabbit(adjective) {
this.adjective = adjective;
this.describeMyself = function() {
console.log("I am a " + this.adjective + " rabbit");
};
}
var rabbit1 = new Rabbit("fluffy");
var rabbit2 = new Rabbit("happy");
var rabbit3 = new Rabbit("sleepy");
// Our person constructor
function Person (name, age) {
this.name = name;
this.age = age;
}
// Now we can make an array of people
var family = new Array();
family[0] = new Person("alice", 40);
family[1] = new Person("bob", 42);
family[2] = new Person("michelle", 8);
family[3] = new Person("timmy", 6);
// add the last family member, "timmy", who is 6 years old
//Person Constructor
function Person(name,age) {
this.name = name;
this.age = age;
}
//Array of People
var family = new Array();
family[0] = new Person("alice",40);
family[1] = new Person("bob",42);
family[2] = new Person("michelle",8);
family[3] = new Person("timmy",6);
//Loop Through New Array
for(i=0;i<4;i++) {
console.log(family[i].name)
}
// Our person constructor
function Person (name, age) {
this.name = name;
this.age = age;
}
// We can make a function which takes persons as arguments
// This one computes the difference in ages between two people
var ageDifference = function(person1, person2) {
return person1.age - person2.age;
}
var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);
// get the difference in age between alice and billy using our function
var diff = ageDifference(alice,billy);
// Our person constructor
function Person (name, age) {
this.name = name;
this.age = age;
}
// We can make a function which takes persons as arguments
// This one computes the difference in ages between two people
var ageDifference = function(person1, person2) {
return person1.age - person2.age;
};
var olderAge = function(person1, person2) {
if(person1.age>person2.age) {
return person1.age;
}
else{
return person2.age;
}
}
// Let's bring back alice and billy to test our new function
var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);
console.log("The older person is " + olderAge(alice, billy));
var spencer = {
age: 22,
country: "United States"
};
var spencer2 = new Object();
spencer2.age = 22;
spencer2.country = "United States";
var snoopy = new Object();
snoopy.species = "beagle";
snoopy.age = 10;
// save Snoopy's age and species into variables
// use dot notation for snoopy's species
var species = snoopy.species = "beagle";
// use bracket notation for snoopy's age
var age = snoopy["age"] = 10;
// 3 lines required to make harry_potter
var harry_potter = new Object();
harry_potter.pages = 350;
harry_potter.author = "J.K. Rowling";
// A custom constructor for book
function Book (pages, author) {
this.pages = pages;
this.author = author;
}
// Use our new constructor to make the_hobbit in one line
var the_hobbit = new Book(320,"J.R.R. Tolkien");
function Circle (radius) {
this.radius = radius;
this.area = function () {
return Math.PI * this.radius * this.radius;
};
this.perimeter = function () {
return 2 * Math.PI * this.radius
};
};
var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-777",
email: "bob.jones@example.com"
};
console.log(bob.firstName);
console.log(bob.lastName);
console.log(bob.email);
var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-7777",
email: "bob.jones@example.com"
};
var mary = {
firstName: "Mary",
lastName: "Johnson",
phoneNumber:"(650) 888-8888",
email: "mary.johnson@example.com"
};
var contacts = [bob,mary];
console.log(contacts[1].phoneNumber);
var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-7777",
email: "bob.jones@example.com"
};
var mary = {
firstName: "Mary",
lastName: "Johnson",
phoneNumber: "(650) 888-8888",
email: "mary.johnson@example.com"
};
var contacts = [bob, mary];
var printPerson = function(person){
console.log(person.firstName + " " + person.lastName);
};
printPerson(contacts[0]);
printPerson(contacts[1]);
var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-7777",
email: "bob.jones@example.com"
};
var mary = {
firstName: "Mary",
lastName: "Johnson",
phoneNumber: "(650) 888-8888",
email: "mary.johnson@example.com"
};
var contacts = [bob, mary];
function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}
function list(){
var contactsLength = contacts.length;
for(i = 0;i<contactsLength;i++){
printPerson(contacts[i])
}
}
list();
var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-7777",
email: "bob.jones@example.com"
};
var mary = {
firstName: "Mary",
lastName: "Johnson",
phoneNumber: "(650) 888-8888",
email: "mary.johnson@example.com"
};
var contacts = [bob, mary];
function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}
function list() {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
printPerson(contacts[i]);
}
}
function search(lastName){
var contactsLength = contacts.length;
for(i=0;i<contactsLength;i++){
if(this.lastName=contacts[i].lastName){
printPerson(contacts[i]);
}
}
}
search("Jones");
var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-7777",
email: "bob.jones@example.com"
};
var mary = {
firstName: "Mary",
lastName: "Johnson",
phoneNumber: "(650) 888-8888",
email: "mary.johnson@example.com"
};
var contacts = [bob, mary];
function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}
function list() {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
printPerson(contacts[i]);
}
}
function search(lastName){
var contactsLength = contacts.length;
for(i=0;i<contactsLength;i++){
if(this.lastName=contacts[i].lastName){
printPerson(contacts[i]);
}
}
}
function add(firstName,lastName,phoneNumber,email){
contacts[contacts.length] = {
firstName: firstName,
lastName: lastName,
phoneNumber: phoneNumber,
email: email
};
}
add("John","Gaidimas",2222222222,"john@gaidimas.com");
search("Jones");