Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- // -----------------------------------------------
- // Name: Kean Mattingly
- // -----------------------------------------------
- // -----------------------------------------------
- // Exercise: Property Path Evaluation
- // -----------------------------------------------
- function propertyValueAt(jsObject, objProperty) {
- var i = 0, nestedJsObject = jsObject;
- do {
- nestedJsObject = nestedJsObject[objProperty[i++]];
- } while (i < objProperty.length, nestedJsObject);
- return nestedJsObject;
- }
- // -----------------------------------------------
- // Exercise: Sum Nested Arrays
- // -----------------------------------------------
- function sumNested(numArray) {
- var accumulator = 0;
- for (var i = 0;i < numArray.length;i++) {
- if (numArray[i].constructor == Array) {
- accumulator += sumNested(numArray[i]);
- } else if (numArray[i] == Number) {
- accumulator += numArray[i];
- }
- }
- return accumulator;
- }
- var numArray = [1,2,3,4,[1,2,3,[1,2,3], 2], 4];
- // -----------------------------------------------
- // Exercise: Word Count
- // -----------------------------------------------
- function wordCount(sentence) {
- var possibleWords = sentence.trim().split(" ");
- var wordCount = 0;
- for (var i = 0; i < possibleWords.length; i++) {
- if (!!possibleWords[i].trim()) {
- wordCount++;
- }
- }
- return wordCount;
- }
- // -----------------------------------------------
- // Anagram Tester
- // -----------------------------------------------
- function areTheseAnagrams(firstString, secondString) {
- if (firstString.length !== secondString.length) return false;
- for (var i = 0; i < firstString.length; i++) {
- secondString = secondString.replace(firstString.charAt(i), '');
- }
- return secondString.length > 0 ? false : true;
- }
- // -----------------------------------------------
- // Analyze Prices
- // -----------------------------------------------
- //this is a sin, I know
- function analyzePrices(priceArray) {
- var differenceArray = [];
- for (var i = 0; i < priceArray.length; i++) {
- var largestDifference = 0;
- var indexOfLargest = i;
- var indexOfSmallest = i;
- for (var j = i; j < priceArray.length; j++) {
- if (priceArray[j] - priceArray[i] > largestDifference) {
- largestDifference = priceArray[j] - priceArray[i];
- indexOfLargest = j;
- }
- }
- differenceArray.push({
- diff: largestDifference,
- largest: indexOfLargest,
- smallest: indexOfSmallest
- })
- }
- var bestBuy = differenceArray[0];
- for (var k = 1; k < differenceArray.length; k++) {
- if (bestBuy.diff < differenceArray[k].diff) {
- bestBuy = differenceArray[k];
- }
- }
- return {
- buyIndex: bestBuy.smallest,
- sellIndex: bestBuy.largest
- }
- }
- // -----------------------------------------------
- // Exercise: Fizz Buzz
- // -----------------------------------------------
- function fizzBuzz(num) {
- var fizzBuzzString = '';
- if (num <= 0) return fizzBuzzString;
- for (var i = 1; i <= num; i++) {
- fizzBuzzString += i;
- if (i % 3 == 0 && i > 0) {
- fizzBuzzString += 'fizz';
- }
- if (i % 5 == 0 > 0) {
- fizzBuzzString += 'buzz';
- }
- fizzBuzzString += i == num ? '' : ', ';
- }
- return fizzBuzzString;
- }
- // -----------------------------------------------
- // Exercise: Object Oriented Programming - Car
- // -----------------------------------------------
- function Car() {
- this.speed = 0;
- this.getSpeed = function() {
- return this.speed;
- }
- this.setSpeed = function(speed) {
- this.speed = speed;
- }
- this.stop = function() {
- this.speed = 0;
- }
- }
- // -----------------------------------------------
- // Exercise:
- // -----------------------------------------------
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">// -----------------------------------------------
- // Name: Kean Mattingly
- // -----------------------------------------------
- // -----------------------------------------------
- // Exercise: Property Path Evaluation
- // -----------------------------------------------
- function propertyValueAt(jsObject, objProperty) {
- var i = 0, nestedJsObject = jsObject;
- do {
- nestedJsObject = nestedJsObject[objProperty[i++]];
- } while (i < objProperty.length, nestedJsObject);
- return nestedJsObject;
- }
- // -----------------------------------------------
- // Exercise: Sum Nested Arrays
- // -----------------------------------------------
- function sumNested(numArray) {
- var accumulator = 0;
- for (var i = 0;i < numArray.length;i++) {
- if (numArray[i].constructor == Array) {
- accumulator += sumNested(numArray[i]);
- } else if (numArray[i] == Number) {
- accumulator += numArray[i];
- }
- }
- return accumulator;
- }
- var numArray = [1,2,3,4,[1,2,3,[1,2,3], 2], 4];
- // -----------------------------------------------
- // Exercise: Word Count
- // -----------------------------------------------
- function wordCount(sentence) {
- var possibleWords = sentence.trim().split(" ");
- var wordCount = 0;
- for (var i = 0; i < possibleWords.length; i++) {
- if (!!possibleWords[i].trim()) {
- wordCount++;
- }
- }
- return wordCount;
- }
- // -----------------------------------------------
- // Anagram Tester
- // -----------------------------------------------
- function areTheseAnagrams(firstString, secondString) {
- if (firstString.length !== secondString.length) return false;
- for (var i = 0; i < firstString.length; i++) {
- secondString = secondString.replace(firstString.charAt(i), '');
- }
- return secondString.length > 0 ? false : true;
- }
- // -----------------------------------------------
- // Analyze Prices
- // -----------------------------------------------
- //this is a sin, I know
- function analyzePrices(priceArray) {
- var differenceArray = [];
- for (var i = 0; i < priceArray.length; i++) {
- var largestDifference = 0;
- var indexOfLargest = i;
- var indexOfSmallest = i;
- for (var j = i; j < priceArray.length; j++) {
- if (priceArray[j] - priceArray[i] > largestDifference) {
- largestDifference = priceArray[j] - priceArray[i];
- indexOfLargest = j;
- }
- }
- differenceArray.push({
- diff: largestDifference,
- largest: indexOfLargest,
- smallest: indexOfSmallest
- })
- }
- var bestBuy = differenceArray[0];
- for (var k = 1; k < differenceArray.length; k++) {
- if (bestBuy.diff < differenceArray[k].diff) {
- bestBuy = differenceArray[k];
- }
- }
- return {
- buyIndex: bestBuy.smallest,
- sellIndex: bestBuy.largest
- }
- }
- // -----------------------------------------------
- // Exercise: Fizz Buzz
- // -----------------------------------------------
- function fizzBuzz(num) {
- var fizzBuzzString = '';
- if (num <= 0) return fizzBuzzString;
- for (var i = 1; i <= num; i++) {
- fizzBuzzString += i;
- if (i % 3 == 0 && i > 0) {
- fizzBuzzString += 'fizz';
- }
- if (i % 5 == 0 > 0) {
- fizzBuzzString += 'buzz';
- }
- fizzBuzzString += i == num ? '' : ', ';
- }
- return fizzBuzzString;
- }
- // -----------------------------------------------
- // Exercise: Object Oriented Programming - Car
- // -----------------------------------------------
- function Car() {
- this.speed = 0;
- this.getSpeed = function() {
- return this.speed;
- }
- this.setSpeed = function(speed) {
- this.speed = speed;
- }
- this.stop = function() {
- this.speed = 0;
- }
- }
- // -----------------------------------------------
- // Exercise:
- // -----------------------------------------------
- </script></body>
- </html>
Add Comment
Please, Sign In to add comment