Created
April 28, 2016 00:01
-
-
Save jennifer-shehane/8b3c0c76dfb8c277428e79c0e0d0796f to your computer and use it in GitHub Desktop.
Intro to JS in-class exercises
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Test Page</title> | |
</head> | |
<body> | |
<p>This is awesome JS code.</p> | |
<script src="mycode.js"></script> | |
</body> | |
</html> |
This file contains 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
alert('Hello World!'); | |
console.log('Secret message'); // This logs a secret message. | |
document.write('Another message'); | |
var favoriteDessert = "Ice Cream"; | |
console.log(favoriteDessert); | |
var jensSlices = 1; | |
var briansSlices = 3; | |
var totalSlices = jensSlices + briansSlices; | |
document.write("Number of things " + totalSlices); | |
// log a greeting with the user's name | |
var firstName = "Jennifer"; | |
console.log("Hello " + firstName); | |
// log the cute kitten's name | |
var kittensName = "Admiral <br>"; | |
kittensName += "Snuggles"; | |
console.log(kittensName); | |
document.write("<p>" + kittensName + "</p>"); | |
var billAmount = 35; | |
var percent = .20; | |
var totalTip = percent * billAmount; | |
var total = billAmount + totalTip; | |
document.write("Your bill is: $" + total); | |
var inputCelsius = 25; | |
var outputFahrenheit = (inputCelsius * 9)/5 + 32; | |
document.write(inputCelsius + '\xB0C is ' + outputFahrenheit + '\xB0F'); | |
var inputFahrenheit = 73; | |
var outputCelsius = (inputFahrenheit - 32)/9 * 5; | |
document.write(inputFahrenheit + '\u00B0F is ' + outputCelsius + '\u00B0C.'); | |
function gdiSentence() { | |
console.log("This sentence is in a function"); | |
} | |
gdiSentence(); | |
function fullName(firstName, lastName) { | |
// console.log("My name is " + firstName + " " + lastName); | |
return firstName + " " + lastName; | |
} | |
var wholeName = fullName("Jennifer", "Mann"); | |
console.log("My name is " + wholeName); | |
function square(num) { | |
return (num * num); | |
console.log("another thing in the function"); | |
} | |
console.log(square(7)); | |
function whatIsAwesome() { | |
var awesomeGroup = 'Girl Develop It'; // Local scope | |
console.log(awesomeGroup + ' is pretty awesome.'); // Will work | |
} | |
whatIsAwesome(); | |
console.log(awesomeGroup + ' is pretty awesome.'); // Won't work | |
if (temp < 50) { | |
// do this | |
} else if (temp < 30) { | |
// do this | |
} | |
var temp = Math.random() * 100; | |
// if (temp < 0) { | |
// document.write("Stay inside!"); | |
// } else if (temp < 30) { | |
// document.write("Wear a coat and a hat"); | |
// } else if (temp < 50) { | |
// document.write("Wear a coat"); | |
// } else { | |
// document.write("Wear whatever you want!"); | |
// } | |
console.log(temp); | |
if (temp >=0 && temp < 50) { | |
document.write("Wear a coat"); | |
} | |
if (temp < -50) { | |
document.write("You're in the arctic"); | |
} | |
function bigNum(nom1, nom2) { | |
if (nom1 === nom2) { | |
document.write('These numbers are exactly equal to each other. They are both ' + nom1); | |
} else if (nom1 > nom2) { | |
document.write('<br>'); | |
document.write('First number ' + nom1 + ' is greater than second number ' +nom2 + ' '); | |
} else if (nom2 > nom1) { | |
document.write('<br>'); | |
document.write('Second number ' + nom2 + ' is greater than ' + nom1); | |
} else { | |
document.write('<br>'); | |
document.write('There is no comparison between ' + nom1 + ' and ' + nom2); | |
return undefined; | |
} | |
} | |
// console.log(bigNum(4, 4)); | |
// document.write(bigNum(a, -10)); | |
bigNum(10, - 10); | |
bigNum(10, 7); | |
bigNum(1, -7); | |
bigNum(Math.random(), Math.random()); | |
bigNum(90, 'b'); | |
bigNum('apples', 'oranges'); | |
// will count 1 to 10 | |
for (var i = 1; i <= 10; i++) { | |
debugger | |
console.log(i); | |
} | |
// Count from 1 to 50 | |
for (var i = 1; i <= 50; i++) { | |
// Says 'Fizz' after multiples of three | |
if (i % 3 == 0) { | |
console.log(' Fizz'); | |
} else if (i % 5 == 0) { | |
// Says 'Buzz' after multiples of five | |
console.log(' Buzz'); | |
} else { | |
console.log(i); | |
} | |
} | |
// Count from 100 to 200 | |
for (var i = 100; i <= 200; i++) { | |
console.log('Testing ' + i); | |
//Stop at the first multiple of 7 | |
if (i % 7 == 0) { | |
console.log('Found it! ' + i); | |
break; | |
} | |
} | |
for(var i = 1; i <= 12; i++) { | |
var answer = 9 * i; | |
console.log("9 x " + i + " = " + answer); | |
} | |
for(var startingNum = 1; startingNum <= 12; startingNum++) { | |
for(var j = 1; j <= 12; j++) { | |
debugger; | |
var x = j * i; | |
document.write(i + "x" + j + "=" + x + "<br>"); | |
} | |
} | |
var directors = ['Coppola', 'Miyazaki', 'Kubrick']; | |
console.log(directors.length); | |
var favoriteFoods = ["pizza", "taco", "lasagna"]; | |
for(i = 0; i < favoriteFoods.length; i++) { | |
debugger | |
console.log("My favorite food is " + favoriteFoods[i]); | |
} | |
var albertTheParrot = { | |
age: 21, | |
color: 'white', | |
talk: function() { | |
console.log('Hello!'); | |
}, | |
eat: function(food) { | |
console.log('Yum, I love ' + food); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment