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: Brad Francis
- //---------------------------------------
- //---------------------------------------
- // Exercise: Property Path Evaluation
- //---------------------------------------
- function propertyValueAt(object, attributes) {
- var vals = [];
- attributes.forEach(function(attr){
- if(Array.isArray(attr))
- propertValueAtRec(vals, attr, attributes);
- else
- vals.push(object[attr]);
- });
- return vals;
- }
- function propertValueAtRec(vals, object, attributes) {
- attributes.forEach(function(attr){
- if(typeof attr === "object")
- propertValueAtRec(vals, attr, attributes);
- else
- vals.push(object[attr]);
- });
- return vals;
- }
- //---------------------------------------
- // Exercise: Sum Nested Arrays
- //---------------------------------------
- var sum;
- function sumNested(arr) {
- sum = 0;
- sumNestedRec(arr);
- return sum;
- }
- function sumNestedRec(arr) {
- arr.forEach(function(el){
- if(Array.isArray(el))
- sumNestedRec(el);
- else
- sum += el;
- });
- }
- //---------------------------------------
- // Exercise: Word Count
- //---------------------------------------
- function wordCount(sentence) {
- var midWord = false;
- var count = 0;
- var letters = sentence.split("");
- letters.forEach(function(char){
- if(char === ' ') // if space
- midWord = false;
- else if (!midWord){ // if new word
- count++;
- midWord = true;
- }
- });
- return count;
- }
- //---------------------------------------
- // Exercise: Anagram Tester
- //---------------------------------------
- function areTheseAnagrams(word1, word2) {
- var isAnagram = false;
- var isInBoth = false;
- var word1letters = word1.split("");
- var word2letters = word2.split("");
- for(var i in word1letters) {
- isInBoth = false;
- for(var j in word2letters) {
- if(word1letters[i] === word2letters[j])
- isInBoth = true;
- }
- if(isInBoth)
- isAnagram = true;
- else
- return false;
- }
- return isAnagram;
- }
- //---------------------------------------
- // Exercise: Analyze Prices
- //---------------------------------------
- function analyzePrices(prices) {
- var analysis = new Object();
- /*
- var buy, sell;
- while ((buy = calcBuyIndex(prices)) === null) {}
- while ((sell = calcSellIndex(prices)) === null) {}
- if(buy === null || sell === null) {
- analysis.buyIndex = null;
- analysis.sellIndex = null;
- }
- else {
- analysis.buyIndex = buy;
- analysis.sellIndex = sell;
- }
- */
- var maxProfit = 0;
- var buyInd;
- var sellInd;
- var currProfit;
- for(var i = 0; i < prices.length; i++){
- for(var j = 0; j < prices.length; j++){
- currProfit = prices[j] - prices[i];
- if(currProfit > maxProfit && i < j){
- maxProfit = currProfit;
- buyInd = i;
- sellInd = j;
- }
- }
- }
- if (maxProfit === 0){
- buyInd = null;
- sellInd = null;
- }
- analysis.buyIndex = buyInd;
- analysis.sellIndex = sellInd;
- return analysis;
- }
- /*
- function indexOfMin(arr){
- var minIndex = 0;
- for (var i in arr){
- if (arr[i] < arr[minIndex])
- minIndex = i;
- }
- }
- function indexOfMax(arr){
- var maxIndex = 0;
- for (var i in arr){
- if (arr[i] > arr[maxIndex])
- maxIndex = i;
- }
- }
- function calcBuyIndex(arr){
- var minIndex = indexOfMin(arr);
- var maxIndex = indexOfMax(arr);
- if (maxIndex <= minIndex){
- arr.splice(minIndex, 1);
- return null;
- }
- else
- return minIndex;
- }
- function calcSellIndex(arr){
- var minIndex = indexOfMin(arr);
- var maxIndex = indexOfMax(arr);
- if (maxIndex <= minIndex){
- arr.splice(maxIndex, 1);
- return null;
- }
- else
- return maxIndex;
- }
- */
- //---------------------------------------
- // Exercise: Fizz Buzz
- //---------------------------------------
- function fizzBuzz(n) {
- var output = "";
- if (n > 0) {
- for (var i = 1; i <= n; i++) {
- output += i;
- if(i % 3 === 0)
- output += "fizz";
- if(i % 5 === 0)
- output += "buzz";
- if (i < n)
- output += ", ";
- }
- }
- return output;
- }
- //---------------------------------------
- // Exercise: OOP - Car
- //---------------------------------------
- class Car {
- constructor(){
- this.speed = 0;
- }
- getSpeed() { return this.speed; }
- setSpeed(speed) { this.speed = speed; }
- stop() { this.speed = 0; }
- }
- //---------------------------------------
- // Bonus: OCalculate Bowling Score
- //---------------------------------------
- function calculateBowlingScores(scores){
- var score = 0;
- var scoreLetters = scores.split("");
- scoreLetters.forEach(function(el){
- switch (el) {
- case 'X':
- score += 20;
- break;
- case '/':
- score += 10;
- break;
- case '-':
- score += 0;
- break;
- default:
- score += parseInt(el);
- break;
- }
- });
- return score;
- }
- //---------------------------------------
- // Testing Code
- //---------------------------------------
- // EXE 1 - Property Path
- console.log("\nEXE 1 - Property Path\n");
- var object = {
- a: 1,
- b: {
- c: 2,
- d: 3
- }
- }
- console.log(propertyValueAt(object, ['a']));
- console.log(propertyValueAt(object, ['b']));
- console.log(propertyValueAt(object, ['b'], ['d']));
- console.log(propertyValueAt(object, ['d']));
- console.log(propertyValueAt(object, ['q']));
- // EXE 2 - Sum Nested
- console.log("\nEXE 2 - Sum Nested\n");
- console.log(sumNested([1, 1, 1, [3, 4, [8]], [5]]));
- console.log(sumNested([]));
- // EXE 3 - Word Count
- console.log("\nEXE 3 - Word Count\n");
- console.log(wordCount('This is a short sentence!'));
- console.log(wordCount('Thisisa$!longword!'));
- console.log(wordCount(' '));
- // EXE 4 - Anagram
- console.log("\nEXE 4 - Anagram\n");
- console.log(areTheseAnagrams('abc', 'bca'));
- console.log(areTheseAnagrams('abc', 'cde'));
- // EXE 5 - Analyze Prices
- console.log("\nEXE 5 - Analyze Prices\n");
- var prices = [1,2,3,4,5,6,7,8,9,0];
- console.log(analyzePrices(prices));
- prices = [8,4,3,1,5,5,2,8,2,3];
- console.log(analyzePrices(prices));
- prices = [5,2,5,4,5,6,3,8,1,8];
- console.log(analyzePrices(prices));
- prices = [1,1,1,1,1];
- console.log(analyzePrices(prices));
- // EXE 6 - FizzBuzz
- console.log("\nEXE 6 - FizzBuzz\n");
- console.log(fizzBuzz(0));
- console.log(fizzBuzz(15));
- // EXE 7 - Car
- console.log("\nEXE 7 - Car\n");
- var car = new Car();
- console.log(car.getSpeed());
- car.setSpeed(10);
- console.log(car.getSpeed());
- car.stop();
- console.log(car.getSpeed());
- // BONUS 1 - Calc Bowling Scores
- console.log("\nBONUS 1 - Calc Bowling Scores\n");
- var game1 = "XXXXXXXXXX";
- console.log(calculateBowlingScores(game1));
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">//---------------------------------------
- // Name: Brad Francis
- //---------------------------------------
- //---------------------------------------
- // Exercise: Property Path Evaluation
- //---------------------------------------
- function propertyValueAt(object, attributes) {
- var vals = [];
- attributes.forEach(function(attr){
- if(Array.isArray(attr))
- propertValueAtRec(vals, attr, attributes);
- else
- vals.push(object[attr]);
- });
- return vals;
- }
- function propertValueAtRec(vals, object, attributes) {
- attributes.forEach(function(attr){
- if(typeof attr === "object")
- propertValueAtRec(vals, attr, attributes);
- else
- vals.push(object[attr]);
- });
- return vals;
- }
- //---------------------------------------
- // Exercise: Sum Nested Arrays
- //---------------------------------------
- var sum;
- function sumNested(arr) {
- sum = 0;
- sumNestedRec(arr);
- return sum;
- }
- function sumNestedRec(arr) {
- arr.forEach(function(el){
- if(Array.isArray(el))
- sumNestedRec(el);
- else
- sum += el;
- });
- }
- //---------------------------------------
- // Exercise: Word Count
- //---------------------------------------
- function wordCount(sentence) {
- var midWord = false;
- var count = 0;
- var letters = sentence.split("");
- letters.forEach(function(char){
- if(char === ' ') // if space
- midWord = false;
- else if (!midWord){ // if new word
- count++;
- midWord = true;
- }
- });
- return count;
- }
- //---------------------------------------
- // Exercise: Anagram Tester
- //---------------------------------------
- function areTheseAnagrams(word1, word2) {
- var isAnagram = false;
- var isInBoth = false;
- var word1letters = word1.split("");
- var word2letters = word2.split("");
- for(var i in word1letters) {
- isInBoth = false;
- for(var j in word2letters) {
- if(word1letters[i] === word2letters[j])
- isInBoth = true;
- }
- if(isInBoth)
- isAnagram = true;
- else
- return false;
- }
- return isAnagram;
- }
- //---------------------------------------
- // Exercise: Analyze Prices
- //---------------------------------------
- function analyzePrices(prices) {
- var analysis = new Object();
- /*
- var buy, sell;
- while ((buy = calcBuyIndex(prices)) === null) {}
- while ((sell = calcSellIndex(prices)) === null) {}
- if(buy === null || sell === null) {
- analysis.buyIndex = null;
- analysis.sellIndex = null;
- }
- else {
- analysis.buyIndex = buy;
- analysis.sellIndex = sell;
- }
- */
- var maxProfit = 0;
- var buyInd;
- var sellInd;
- var currProfit;
- for(var i = 0; i < prices.length; i++){
- for(var j = 0; j < prices.length; j++){
- currProfit = prices[j] - prices[i];
- if(currProfit > maxProfit && i < j){
- maxProfit = currProfit;
- buyInd = i;
- sellInd = j;
- }
- }
- }
- if (maxProfit === 0){
- buyInd = null;
- sellInd = null;
- }
- analysis.buyIndex = buyInd;
- analysis.sellIndex = sellInd;
- return analysis;
- }
- /*
- function indexOfMin(arr){
- var minIndex = 0;
- for (var i in arr){
- if (arr[i] < arr[minIndex])
- minIndex = i;
- }
- }
- function indexOfMax(arr){
- var maxIndex = 0;
- for (var i in arr){
- if (arr[i] > arr[maxIndex])
- maxIndex = i;
- }
- }
- function calcBuyIndex(arr){
- var minIndex = indexOfMin(arr);
- var maxIndex = indexOfMax(arr);
- if (maxIndex <= minIndex){
- arr.splice(minIndex, 1);
- return null;
- }
- else
- return minIndex;
- }
- function calcSellIndex(arr){
- var minIndex = indexOfMin(arr);
- var maxIndex = indexOfMax(arr);
- if (maxIndex <= minIndex){
- arr.splice(maxIndex, 1);
- return null;
- }
- else
- return maxIndex;
- }
- */
- //---------------------------------------
- // Exercise: Fizz Buzz
- //---------------------------------------
- function fizzBuzz(n) {
- var output = "";
- if (n > 0) {
- for (var i = 1; i <= n; i++) {
- output += i;
- if(i % 3 === 0)
- output += "fizz";
- if(i % 5 === 0)
- output += "buzz";
- if (i < n)
- output += ", ";
- }
- }
- return output;
- }
- //---------------------------------------
- // Exercise: OOP - Car
- //---------------------------------------
- class Car {
- constructor(){
- this.speed = 0;
- }
- getSpeed() { return this.speed; }
- setSpeed(speed) { this.speed = speed; }
- stop() { this.speed = 0; }
- }
- //---------------------------------------
- // Bonus: OCalculate Bowling Score
- //---------------------------------------
- function calculateBowlingScores(scores){
- var score = 0;
- var scoreLetters = scores.split("");
- scoreLetters.forEach(function(el){
- switch (el) {
- case 'X':
- score += 20;
- break;
- case '/':
- score += 10;
- break;
- case '-':
- score += 0;
- break;
- default:
- score += parseInt(el);
- break;
- }
- });
- return score;
- }
- //---------------------------------------
- // Testing Code
- //---------------------------------------
- // EXE 1 - Property Path
- console.log("\nEXE 1 - Property Path\n");
- var object = {
- a: 1,
- b: {
- c: 2,
- d: 3
- }
- }
- console.log(propertyValueAt(object, ['a']));
- console.log(propertyValueAt(object, ['b']));
- console.log(propertyValueAt(object, ['b'], ['d']));
- console.log(propertyValueAt(object, ['d']));
- console.log(propertyValueAt(object, ['q']));
- // EXE 2 - Sum Nested
- console.log("\nEXE 2 - Sum Nested\n");
- console.log(sumNested([1, 1, 1, [3, 4, [8]], [5]]));
- console.log(sumNested([]));
- // EXE 3 - Word Count
- console.log("\nEXE 3 - Word Count\n");
- console.log(wordCount('This is a short sentence!'));
- console.log(wordCount('Thisisa$!longword!'));
- console.log(wordCount(' '));
- // EXE 4 - Anagram
- console.log("\nEXE 4 - Anagram\n");
- console.log(areTheseAnagrams('abc', 'bca'));
- console.log(areTheseAnagrams('abc', 'cde'));
- // EXE 5 - Analyze Prices
- console.log("\nEXE 5 - Analyze Prices\n");
- var prices = [1,2,3,4,5,6,7,8,9,0];
- console.log(analyzePrices(prices));
- prices = [8,4,3,1,5,5,2,8,2,3];
- console.log(analyzePrices(prices));
- prices = [5,2,5,4,5,6,3,8,1,8];
- console.log(analyzePrices(prices));
- prices = [1,1,1,1,1];
- console.log(analyzePrices(prices));
- // EXE 6 - FizzBuzz
- console.log("\nEXE 6 - FizzBuzz\n");
- console.log(fizzBuzz(0));
- console.log(fizzBuzz(15));
- // EXE 7 - Car
- console.log("\nEXE 7 - Car\n");
- var car = new Car();
- console.log(car.getSpeed());
- car.setSpeed(10);
- console.log(car.getSpeed());
- car.stop();
- console.log(car.getSpeed());
- // BONUS 1 - Calc Bowling Scores
- console.log("\nBONUS 1 - Calc Bowling Scores\n");
- var game1 = "XXXXXXXXXX";
- console.log(calculateBowlingScores(game1));
- </script></body>
- </html>
Add Comment
Please, Sign In to add comment