This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var power = function(base, exponent) { | |
if (exponent === 0) { | |
return 1; | |
} | |
else { | |
return base * power (base, exponent - 1); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Accepts a number x as input and returns its square | |
var square = function (x) { | |
return x * x; | |
}; | |
// Accepts a number x as input and returns its cube | |
var cube = function (x) { | |
return x * x *x; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// calculates taxi fare based upon miles traveled | |
// and the hour of the day in military time (0-23). | |
var taxiFare = function (milesTraveled, pickupTime) { | |
var baseFare = 2.50; | |
var costPerMile = 2.00; | |
var nightSurcharge = 0.50; // 8pm to 6am, every night | |
var cost = baseFare + (costPerMile * milesTraveled); | |
// add the nightSurcharge to the cost if it is after |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var quarter = function(number) { | |
return number/4; | |
} | |
if (quarter(24) % 3 === 0 ) { | |
console.log("The statement is true"); | |
} else { | |
console.log("The statement is false"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
//The Game | |
//Each player chooses either rock, paper or scissors | |
//Rock destroys scissors | |
//Scissors cut paper | |
//Paper covers rock | |
//Phases | |
//1. User makes a choice | |
//2. Computer makes a choice |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var numSheep = 4; | |
var monthNumber = 1; | |
var monthsToPrint = 12; | |
while(monthNumber <= monthsToPrint) { | |
numSheep *= 4; | |
console.log("There will be " + numSheep + " sheep after " + monthNumber + " month(s)!"); | |
monthNumber++; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Syntax of a for loop: | |
for ( *start with this* ; *loop if this expression is true* ; *do this after each loop*) { | |
* in each loop, d this code!* | |
} | |
In order (initialize; check condition; control the loop) */ | |
//Not operational cars | |
var carsOperational = 10; | |
var totalCars = 14; | |
for (var stoppedCar = carsOperational + 1; stoppedCar <= totalCars; stoppedCar++ ) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var totalCars = 10; | |
var carsOperational = 8; | |
var carNumber = 1; | |
while(carNumber <= carsOperational) { | |
console.log("Car #" + carNumber + " is running."); | |
carNumber++; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var totalCars = 10; | |
var carsOperational = 8; | |
var carNumber = 1; | |
while(carNumber <= carsOperational) { | |
console.log("Car #" + carNumber + " is running."); | |
carNumber++; | |
} | |
for( var stoppedCar = carsOperational + 1; stoppedCar <= totalCars; stoppedCar++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for(var number = 6; number > 0; number--) { | |
console.log(number); | |
} |