Advertisement
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: Brian Pendleton
- //--------------------------------------
- //--------------------------------------
- //Exercise: Property Path Evaluation
- //take in an object and an array of a particular property of that object.
- function propertyValueAt(obj, arr) {
- //if the property passed in is not undefined, return it, otherwise return undefined
- if(arr !== undefined) {
- return arr;
- }
- else {
- return undefined;
- }
- }
- //--------------------------------------
- //--------------------------------------
- //Exercise: Sum Nested Arrays
- //attempts to flatten the array and then add all elements...
- // function sumNested(arr) {
- // let sum = 0;
- // let escape = true;
- //if the "every" function finds something that isn't a number (like an array) flatten again
- // while(escape) {
- // let flattened = arr.reduce(flattener(), []);
- // // // escape = arr.every(function(val) {
- // // // if(typeof val !== 'number') {
- // // // // return false;
- // // // }
- // // // });
- // // escape = false;
- // //
- // // }
- //using an accumulator, add things together
- // sum = flattened.reduce(function(accumulator, currentVal) {
- // return accumulator + currentVal;
- // });
- // return sum;
- // }
- // function flattener(accumulator, currentVal) {
- // return accumulator.concat(currentVal);
- // };
- // //should be 20
- // let testArr = [5, 5, 5, [1, 2, [3, 4]]];
- // console.log(sumNested(testArr));
- //--------------------------------------
- //--------------------------------------
- //Exercise: Word Count
- //Takes in a string input
- function wordCount(inputSentence) {
- //return the length of the array once split by spaces
- return inputSentence.split(" ").length;
- }
- //--------------------------------------
- //--------------------------------------
- //Exercise: Anagram Tester
- function areTheseAnagrams(string1, string2) {
- let allKeys = [];
- let numKeys = [];
- let allKeys2 = [];
- let numKeys2 = [];
- let isAnagram = false;
- string1 = string1.toLowerCase();
- string2 = string2.toLowerCase();
- //checks if they're the same length
- if(string1.length !== string2.length) {
- return false;
- }
- for(i = 0; i < string1.length; i++) {
- if(!allKeys.includes(string1[i])) {
- allKeys.push(string1[i]);
- numKeys.push(1);
- }
- else if(allKeys.includes(string1[i])) {
- let index = allKeys.indexOf(string1[i]);
- numKeys[index]++;
- }
- }
- for(i = 0; i < string2.length; i++) {
- if(!allKeys2.includes(string2[i])) {
- allKeys2.push(string2[i]);
- numKeys2.push(1);
- }
- else if(allKeys2.includes(string2[i])) {
- let index = allKeys2.indexOf(string2[i]);
- numKeys2[index]++;
- }
- }
- //output both strings and their letter counts
- console.log(allKeys + " " + numKeys);
- console.log(allKeys2 + " " + numKeys2);
- //It should theoretically work, but since the letters aren't entered in the same order, it doesn't evaluate correctly.
- return (allKeys == allKeys2 && numKeys == numKeys2);
- }
- //--------------------------------------
- //--------------------------------------
- //Exercise: Analyze Prices
- function analyzePrices(prices) {
- // var min = 0;
- // var max = 0;
- // var profit = 0;
- var minPrice = prices.reduce(function(a, b) {
- return Math.min(a, b);
- });
- var maxPrice = prices.reduce(function(a, b) {
- return Math.max(a, b);
- });
- //this section was going to be the real solution but I ran out of time so now you can travel through time when you buy/sell.
- // for(i = 0; i < prices.length; i++) {
- // for(j = 1; j < prices.length; j++) {
- // if(prices[i] < prices[j]){
- // if(min < prices[i]) {
- // min = prices[i];
- // }
- // if(max > prices[j];
- // if
- // }
- // }
- // }
- var buyIndex = prices.indexOf(minPrice);
- var sellIndex = prices.indexOf(maxPrice);
- return {
- buyIndex: buyIndex,
- sellIndex: sellIndex
- };
- }
- //--------------------------------------
- //--------------------------------------
- //Exercise: Fizz Buzz
- function fizzBuzz(n) {
- if(n <= 0) {
- return "";
- }
- else if(n > 0) {
- console.log(n);
- //take modulo of 3 and/or 5 where appropriate and output the associated string
- if((n % 3) === 0 && (n % 5) === 0) {
- console.log("fizzbuzz");
- }
- else if((n % 3) === 0) {
- console.log("fizz");
- }
- else if((n % 5) === 0) {
- console.log("buzz");
- }
- }
- }
- //--------------------------------------
- //--------------------------------------
- //Exercise: Object Oriented Programming - Car
- const Car = {
- //default speed value is 0
- _speed: 0,
- getSpeed() {
- return this._speed;
- },
- setSpeed(speed) {
- this._speed = speed;
- },
- stop() {
- this._speed = 0;
- }
- }
- //--------------------------------------
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">//--------------------------------------
- //Name: Brian Pendleton
- //--------------------------------------
- //--------------------------------------
- //Exercise: Property Path Evaluation
- //take in an object and an array of a particular property of that object.
- function propertyValueAt(obj, arr) {
- //if the property passed in is not undefined, return it, otherwise return undefined
- if(arr !== undefined) {
- return arr;
- }
- else {
- return undefined;
- }
- }
- //--------------------------------------
- //--------------------------------------
- //Exercise: Sum Nested Arrays
- //attempts to flatten the array and then add all elements...
- // function sumNested(arr) {
- // let sum = 0;
- // let escape = true;
- //if the "every" function finds something that isn't a number (like an array) flatten again
- // while(escape) {
- // let flattened = arr.reduce(flattener(), []);
- // // // escape = arr.every(function(val) {
- // // // if(typeof val !== 'number') {
- // // // // return false;
- // // // }
- // // // });
- // // escape = false;
- // //
- // // }
- //using an accumulator, add things together
- // sum = flattened.reduce(function(accumulator, currentVal) {
- // return accumulator + currentVal;
- // });
- // return sum;
- // }
- // function flattener(accumulator, currentVal) {
- // return accumulator.concat(currentVal);
- // };
- // //should be 20
- // let testArr = [5, 5, 5, [1, 2, [3, 4]]];
- // console.log(sumNested(testArr));
- //--------------------------------------
- //--------------------------------------
- //Exercise: Word Count
- //Takes in a string input
- function wordCount(inputSentence) {
- //return the length of the array once split by spaces
- return inputSentence.split(" ").length;
- }
- //--------------------------------------
- //--------------------------------------
- //Exercise: Anagram Tester
- function areTheseAnagrams(string1, string2) {
- let allKeys = [];
- let numKeys = [];
- let allKeys2 = [];
- let numKeys2 = [];
- let isAnagram = false;
- string1 = string1.toLowerCase();
- string2 = string2.toLowerCase();
- //checks if they're the same length
- if(string1.length !== string2.length) {
- return false;
- }
- for(i = 0; i < string1.length; i++) {
- if(!allKeys.includes(string1[i])) {
- allKeys.push(string1[i]);
- numKeys.push(1);
- }
- else if(allKeys.includes(string1[i])) {
- let index = allKeys.indexOf(string1[i]);
- numKeys[index]++;
- }
- }
- for(i = 0; i < string2.length; i++) {
- if(!allKeys2.includes(string2[i])) {
- allKeys2.push(string2[i]);
- numKeys2.push(1);
- }
- else if(allKeys2.includes(string2[i])) {
- let index = allKeys2.indexOf(string2[i]);
- numKeys2[index]++;
- }
- }
- //output both strings and their letter counts
- console.log(allKeys + " " + numKeys);
- console.log(allKeys2 + " " + numKeys2);
- //It should theoretically work, but since the letters aren't entered in the same order, it doesn't evaluate correctly.
- return (allKeys == allKeys2 && numKeys == numKeys2);
- }
- //--------------------------------------
- //--------------------------------------
- //Exercise: Analyze Prices
- function analyzePrices(prices) {
- // var min = 0;
- // var max = 0;
- // var profit = 0;
- var minPrice = prices.reduce(function(a, b) {
- return Math.min(a, b);
- });
- var maxPrice = prices.reduce(function(a, b) {
- return Math.max(a, b);
- });
- //this section was going to be the real solution but I ran out of time so now you can travel through time when you buy/sell.
- // for(i = 0; i < prices.length; i++) {
- // for(j = 1; j < prices.length; j++) {
- // if(prices[i] < prices[j]){
- // if(min < prices[i]) {
- // min = prices[i];
- // }
- // if(max > prices[j];
- // if
- // }
- // }
- // }
- var buyIndex = prices.indexOf(minPrice);
- var sellIndex = prices.indexOf(maxPrice);
- return {
- buyIndex: buyIndex,
- sellIndex: sellIndex
- };
- }
- //--------------------------------------
- //--------------------------------------
- //Exercise: Fizz Buzz
- function fizzBuzz(n) {
- if(n <= 0) {
- return "";
- }
- else if(n > 0) {
- console.log(n);
- //take modulo of 3 and/or 5 where appropriate and output the associated string
- if((n % 3) === 0 && (n % 5) === 0) {
- console.log("fizzbuzz");
- }
- else if((n % 3) === 0) {
- console.log("fizz");
- }
- else if((n % 5) === 0) {
- console.log("buzz");
- }
- }
- }
- //--------------------------------------
- //--------------------------------------
- //Exercise: Object Oriented Programming - Car
- const Car = {
- //default speed value is 0
- _speed: 0,
- getSpeed() {
- return this._speed;
- },
- setSpeed(speed) {
- this._speed = speed;
- },
- stop() {
- this._speed = 0;
- }
- }
- //--------------------------------------
- </script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement