Created
June 27, 2012 02:56
-
-
Save Pcushing/3001029 to your computer and use it in GitHub Desktop.
Week3 JS 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
function hello(firstName, lastName) { | |
printResults(["Hello",firstName,lastName].join(' '), "http://socrates.devbootcamp.com/labs/ruby-intro/strings/greeter"); | |
} | |
function favoriteNumber(guess){ | |
myFavoriteNumber = 4; | |
if (guess > myFavoriteNumber) { | |
printResults("Too high, Vi...", "http://socrates.devbootcamp.com/labs/ruby-intro/branching/favorite-number"); | |
} else if (guess < myFavoriteNumber) { | |
printResults("Too low, Joe...", "http://socrates.devbootcamp.com/labs/ruby-intro/branching/favorite-number"); | |
} else { | |
printResults("Boom goes the dynamite. " + guess + " is my favorite number.", "http://socrates.devbootcamp.com/labs/ruby-intro/branching/favorite-number"); | |
} | |
} | |
function pigLatin(word){ | |
var vowels = ['a','e','i','o','u']; | |
numVowels = vowels.length; | |
numWordLetters = word.length; | |
for (var i = 0; i < numWordLetters; i++) { | |
for (var j = 0; j < numVowels; j++){ | |
if (word[i] === vowels[j] && word[i-1] != "q"){ | |
printResults(word.slice(i,numWordLetters) + word.slice(0,i) + "ay", "http://socrates.devbootcamp.com/labs/ruby-intro/branching/pig-latin"); | |
return true; | |
} | |
} | |
} | |
} | |
function leapYear(year){ | |
if (year % 4 != 0){ | |
printResults("False, " + year + " is not a leap year.", "http://socrates.devbootcamp.com/labs/ruby-intro/branching/leap-years"); | |
} else if (year % 400 === 0) { | |
printResults("True, " + year + " is a leap year.", "http://socrates.devbootcamp.com/labs/ruby-intro/branching/leap-years"); | |
} else if (year % 100 === 0) { | |
printResults("False, " + year + " is not a leap year.", "http://socrates.devbootcamp.com/labs/ruby-intro/branching/leap-years"); | |
} else { | |
printResults("True, " + year + " is a leap year.", "http://socrates.devbootcamp.com/labs/ruby-intro/branching/leap-years"); | |
} | |
} | |
function sum(numsArray){ | |
numsArrayLength = numsArray.length; | |
arraySum = 0; | |
for (var i = 0; i < numsArrayLength; i++){ | |
arraySum += numsArray[i]; | |
} | |
printResults(arraySum, "http://socrates.devbootcamp.com/labs/ruby-intro/looping/summing-several-numbers"); | |
} | |
function titleCase(title){ | |
titleArray = title.toLowerCase().split(" "); | |
numWords = titleArray.length; | |
upCasedTitle = ""; | |
for (var i=0; i < numWords; i++){ | |
upCasedTitle += titleArray[i].charAt(0).toUpperCase() + titleArray[i].slice(1) + " "; | |
} | |
printResults(upCasedTitle, "http://socrates.devbootcamp.com/labs/ruby-intro/looping/title-case"); | |
} | |
function factorial(number){ | |
var product = 1; | |
if (number === 0){ | |
printResults(product, "http://socrates.devbootcamp.com/labs/ruby-intro/looping/factorial"); | |
} else { | |
for (var i = number; i > 0; i--){ | |
product *= i; | |
} | |
printResults(product, "http://socrates.devbootcamp.com/labs/ruby-intro/looping/factorial"); | |
} | |
} | |
function smiley(feeling){ | |
if (feeling.mood === "happy"){ | |
printResults(":)", "http://socrates.devbootcamp.com/labs/ruby-intro/hashes/happy-or-sad"); | |
} else if (feeling.mood === "sad"){ | |
printResults(":(", "http://socrates.devbootcamp.com/labs/ruby-intro/hashes/happy-or-sad"); | |
} else { | |
printResults(":|", "http://socrates.devbootcamp.com/labs/ruby-intro/hashes/happy-or-sad"); | |
} | |
} | |
function weather(city){ | |
var cityTemps = { | |
"San Francisco": 60, "Chicago": 30, "Miami": 70, | |
"Los Angeles": 80, "Anchorage": 10 | |
}; | |
if (cityTemps.hasOwnProperty(city)) { | |
printResults("The weather in " + city + " is " + cityTemps[city], "http://socrates.devbootcamp.com/labs/ruby-intro/hashes/weather"); | |
} | |
} | |
function triangle(sides){ | |
a = sides[0]; | |
b = sides[1]; | |
c = sides[2]; | |
if ((a + b <= c ) || (a + c <= b) || (b + c <= a)){ | |
printResults("Invalid triangle.", "http://socrates.devbootcamp.com/labs/ruby-intro/hashes/triangle"); | |
} | |
else if (a === b && a === c) { | |
printResults("Equilateral triangle.", "http://socrates.devbootcamp.com/labs/ruby-intro/hashes/triangle"); | |
} | |
else if ( a === b || a === c || b === c){ | |
printResults("Isosceles triangle.", "http://socrates.devbootcamp.com/labs/ruby-intro/hashes/triangle"); | |
} | |
else { | |
printResults("Scalene triangle.", "http://socrates.devbootcamp.com/labs/ruby-intro/hashes/triangle"); | |
} | |
} | |
//I think, if we're following the syntax exactly from Socrates, this should be fixed to extend/prototype Number. | |
//I only realized this after doing inWords the way this should have been done. | |
function Calculator (){ | |
this.add = function(num1, num2){ | |
printResults(num1+num2, "http://socrates.devbootcamp.com/labs/ruby-intro/classes/instance-method-calculator"); | |
}, | |
this.subtract = function(num1, num2){ | |
printResults(num1-num2, "http://socrates.devbootcamp.com/labs/ruby-intro/classes/instance-method-calculator"); | |
}, | |
this.multiply = function(num1, num2){ | |
printResults(num1*num2, "http://socrates.devbootcamp.com/labs/ruby-intro/classes/instance-method-calculator"); | |
}, | |
this.divide = function(num1, num2){ | |
printResults(num1/num2, "http://socrates.devbootcamp.com/labs/ruby-intro/classes/instance-method-calculator"); | |
} | |
} | |
Number.prototype.inWords = function(){ | |
var numberWords = { | |
1: "one", 2: "two", 3: "three", 4: "four", | |
5: "five", 6: "six", 7: "seven", 8: "eight", | |
9: "nine", 10: "ten", 11: "eleven", 12: "twelve", | |
13: "thirteen", 14: "fourteen", 15: "fifteen", 16: "sixteen", | |
17: "seventeen", 18: "eighteen", 19: "nineteen", 20: "twenty", | |
30: "thirty", 40: "forty", 50: "fifty", 60: "sixty", | |
70: "seventy", 80: "eighty", 90: "ninety", 0: "zero" | |
}; | |
if (numberWords[this]){ | |
printResults(numberWords[this], "http://socrates.devbootcamp.com/labs/ruby-intro/classes/numbers-in-words"); | |
} else if (this < 100 && this >= 20) { | |
printResults(numberWords[Math.floor(this/10)*10] + " " + numberWords[this%10], "http://socrates.devbootcamp.com/labs/ruby-intro/classes/numbers-in-words"); | |
} else if ((this < 1000) && (this % 100 < 20)){ | |
printResults(numberWords[Math.floor(this/100)] + " hundred " + numberWords[this % 100], "http://socrates.devbootcamp.com/labs/ruby-intro/classes/numbers-in-words"); | |
} else if ((this < 1000) && (this % 100 > 20)){ | |
printResults(numberWords[Math.floor(this/100)] + " hundred " + numberWords[(this % 100)-(this % 10)] + " " + numberWords[this % 10], "http://socrates.devbootcamp.com/labs/ruby-intro/classes/numbers-in-words"); | |
} else { | |
printResults("Out of scope for this style question. Come back next time for up to 1 trillion with recursion.", "http://socrates.devbootcamp.com/labs/ruby-intro/classes/numbers-in-words"); | |
} | |
} | |
function Dictionary(){ | |
var words = []; //Couldn't figure out how to get them into an object of objects right away. | |
//Seems like iteration in js would be easier this way too... more likely, I'm just convincing myself that. | |
this.add = function(word){ | |
if (typeof word === "string"){ | |
var wordObj = {}; | |
wordObj[word]= null; | |
words.push(wordObj); | |
printResults(JSON.stringify(words), "http://socrates.devbootcamp.com/labs/ruby-intro/more-classes/creating-a-dictionary-class"); | |
} else { | |
words.push(word); | |
printResults(JSON.stringify(words), "http://socrates.devbootcamp.com/labs/ruby-intro/more-classes/creating-a-dictionary-class"); | |
} | |
}, | |
this.includes = function(word){ | |
for(i=0;i<words.length;i++){ | |
for(var k in words[i]){ | |
if (k === word){ | |
printResults("We found our " + word, "http://socrates.devbootcamp.com/labs/ruby-intro/more-classes/creating-a-dictionary-class"); | |
return true; | |
} | |
} | |
} | |
printResults("That's not in our shark dictionary, but you should add it!", "http://socrates.devbootcamp.com/labs/ruby-intro/more-classes/creating-a-dictionary-class"); | |
}, | |
this.keywords = function(){ | |
for(var i=0; i<words.length; i++){ | |
printResults(Object.keys(words[i]), "http://socrates.devbootcamp.com/labs/ruby-intro/more-classes/creating-a-dictionary-class"); | |
} | |
}, | |
this.entries = function(){ | |
var keys = []; | |
for (i=0;i<words.length;i++){ | |
for(var k in words[i]) { | |
keys.push(k); | |
} | |
} | |
printResults(keys, "http://socrates.devbootcamp.com/labs/ruby-intro/more-classes/creating-a-dictionary-class"); | |
}, | |
this.find = function(string){ | |
var regex = new RegExp("^"+string,"i"); | |
var matches = []; | |
for (i=0;i<words.length;i++){ | |
for(var k in words[i]){ | |
if (k.match(regex)){ | |
matches.push(JSON.stringify(words[i])); | |
} | |
} | |
} | |
printResults(matches, "http://socrates.devbootcamp.com/labs/ruby-intro/more-classes/creating-a-dictionary-class"); | |
} | |
} | |
Array.prototype.newEach = function(callback){ | |
for (i=0; i<this.length;i++){ | |
callback.call(this[i]); | |
} | |
return this; | |
} | |
Array.prototype.newCollect = function(callback){ | |
for (i=0; i<this.length;i++){ | |
this[i] = callback.call(this[i]); | |
debugger; | |
} | |
debugger; | |
return this; | |
} | |
Array.prototype.newCount = function(callback){ | |
var count = 0 | |
for (i=0; i<this.length;i++){ | |
if (callback.call(this[i])){ | |
count +=1 | |
} | |
} | |
return count; | |
} | |
var printResults = function(text, destination){ | |
document.write("<li><a href='" + destination + "'>" + text + "</a></li>"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment