Created
March 22, 2015 18:55
-
-
Save OaklandRocks/fa1d3ba8f7c08a74de3d to your computer and use it in GitHub Desktop.
Some simple JS examples
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 english = {}; | |
| var americanEnglish = Object.create(english); | |
| var britishEnglish = Object.create(english); | |
| var define = function(dictionary,word,definition){ | |
| dictionary[word] = definition; | |
| }; | |
| define(english,"house","place to live"); | |
| define(english,"tree","big green thing"); | |
| define(english,"street","where cars drive"); | |
| define(americanEnglish,"elevator","car to ride up and down"); | |
| define(americanEnglish,"truck","big car"); | |
| define(britishEnglish,"lift","car to ride up and down"); | |
| define(britishEnglish,"lorrie","big car"); | |
| // dictionary | |
| var english = { | |
| entries: {}, | |
| add: function(word,definition) { | |
| english.entries[word] = definition; | |
| }, | |
| doesInclude: function(word) { | |
| return word in english.entries | |
| }, | |
| find: function(substring){ | |
| var results = []; | |
| for(var key in english.entries){ | |
| if(english.entries[key].indexOf(substring) === 0){ | |
| results.push(english.entries[key]); | |
| return results; | |
| } | |
| } | |
| }, | |
| listAll: function(){ | |
| keys(english.entries).sort().forEach(function(e){ | |
| console.log(e, english.entries[e]); | |
| }); | |
| } | |
| }; | |
| english.add("duck","bird") | |
| english.add("ship","on the sea") | |
| // piglatin | |
| var pigLatin = function(word){ | |
| var vowels = /[aeiou]/; | |
| var firstVowel = word.search(vowels); | |
| if (word.substr(0,2) === "qu"){ | |
| return word.substr(2,word.length) + "quay"; | |
| } | |
| if (firstVowel === 0){ | |
| return word + "ay"; | |
| } | |
| else if (firstVowel > 0) | |
| return word.substr(firstVowel,word.length) + word.substr(0,firstVowel) + "ay"; | |
| }; | |
| // mood with object | |
| var smiley = function(object){ | |
| if (object["mood"] === "happy"){ | |
| return ":)"; | |
| } | |
| else if (object["mood"] === "sad"){ | |
| return ":("; | |
| } | |
| else{ | |
| return ":|"; | |
| } | |
| } | |
| // sum numbers | |
| var sumNumbers = function(numbers){ | |
| var size = numbers.length; | |
| i = 0; | |
| var sum = 0; | |
| while (i<size){ | |
| sum = sum + numbers[i]; | |
| i++; | |
| } | |
| return sum | |
| }; | |
| // trianle | |
| var triangle = function(side1,side2,side3){ | |
| var sides = [side1,side2,side3].sort(function(a,b){return a-b}); | |
| if ((sides[0] + sides[1]) <= sides[2]){ | |
| return "invalid"; | |
| } | |
| else if (side1 === side2 && side2 === side3){ | |
| return "equilateral"; | |
| } | |
| else if (side1 === side2 || side2 === side3){ | |
| return "isoceles"; | |
| } | |
| else { | |
| return "scalene"; | |
| } | |
| }; | |
| // iterating examples | |
| var shoppingLists = [ | |
| ["apples", "oranges", "carrots"], | |
| ["ham", "turkey", "cheese"], | |
| ["fruits", "vegetables", "meat"] | |
| ]; | |
| // iterate here | |
| for (var j = 0; j < shoppingLists.length; j += 1) { | |
| var list = shoppingLists[j]; | |
| for (var i = 0; i < list.length; i += 1) { | |
| console.log(list[i]); | |
| } | |
| } | |
| // another example of itteration | |
| var shoppingList = ["apples", "oranges", "carrots"]; | |
| // iterate here | |
| for (var index = 0; index < shoppingList.length; index += 1) { | |
| console.log(shoppingList[index]) | |
| } | |
| // Calculate the sum, mean, and median of an array in JavaScript | |
| var sum = function(array) { | |
| var total = 0; | |
| for (var i=0; i<array.length; i++) { | |
| total += array[i]; | |
| } | |
| return total; | |
| }; | |
| var mean = function(array) { | |
| var arraySum = sum(array); | |
| return arraySum / array.length; | |
| }; | |
| var median = function(array) { | |
| array = array.sort(); | |
| if (array.length % 2 === 0) { // array with even number elements | |
| return (array[array.length/2] + array[(array.length / 2) - 1]) / 2; | |
| } | |
| else { | |
| return array[(array.length - 1) / 2]; // array with odd number elements | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment