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: Zach Burmaster
- //-------------------------------------------------
- //-------------------------------------------------
- //Exercise: Property Path Evaluation
- //-------------------------------------------------
- function propertyValueAt(object, array) {
- //didn't get to this, need to figure out a way to spread the number of things
- //in the array over this below, could do this by looping through and using the same
- // [array[index]] notation on the same thing over and over again
- return object[array[0]][array[1]];
- }
- var object = {a: 1, b: {c: 2, d: 3}};
- console.log(propertyValueAt(object, ['b', 'c']));
- //-------------------------------------------------
- //Exercise: Sum Nested Arrays
- //-------------------------------------------------
- function sumNested(arr) {
- //1 didn't get to checking the number of nested arrays, need to do that using the pseudocode below:
- //2 looping over the length of the array
- //3 take the first thing in the array {
- //4 if it's a number, add it to the newNumber variable {
- //5 else, do what's on line 2 again with this array
- let newArr = arr
- .flat(2)
- .reduce((accumulator, currentValue) => accumulator + currentValue)
- return newArr;
- }
- let testArr = [1, 1, 1, [3, 4, [8]], [5]];
- console.log(sumNested(testArr));;
- //-------------------------------------------------
- //Exercise: Word Count
- //-------------------------------------------------
- function wordCount(sentence) {
- var counter = 0;
- for (var i=0; i<=sentence.length; i++) {
- //feels close here, fails on string of spaces though
- if (sentence[i] == " " && (sentence[i+1] != " " || i == sentence.length)) {
- counter += 1
- }
- }
- return counter+1;
- }
- console.log(wordCount("Thisiashortsentence!"))
- //-------------------------------------------------
- //Exercise: Anagram Tester
- //-------------------------------------------------
- function areTheseAnagrams(str1, str2) {
- let arr1 = []
- for (i=0; i<str1.length; i++) {
- arr1.push(str1[i]);
- }
- let arr2= [];
- for (i=0; i<str2.length; i++) {
- arr2.push(str2[i]);
- }
- let str1Sorted = arr1.sort().join()
- let str2Sorted = arr2.sort().join()
- if (str1Sorted == str2Sorted) {
- return true;
- } else {
- return false;
- }
- }
- console.log(areTheseAnagrams("dingdong", "dongding"));
- //-------------------------------------------------
- //Exercise: Analyze Prices
- //-------------------------------------------------
- //didn't get to this one
- //------------------------------------------------
- //Exercise: Fizz Buzz
- //------------------------------------------------
- function fizzBuzz(n) {
- let answerString = ""
- for (i=0; i<=n; i++) {
- if (i%15 === 0 && i!=0) {
- answerString += i;
- answerString+='fizzbuzz, ';
- } else if (i%5 === 0 & i!=0) {
- answerString += i;
- answerString += 'buzz, ';
- } else if (i%3 === 0 & i!= 0) {
- answerString += i;
- answerString += 'fizz, ';
- } else {
- if (i !== 0) {
- answerString+= i;
- answerString+= ', ';
- }
- }
- }
- // needed to add the case on each if where i is the last number then don't put a comma in,
- // felt like my time is better spent elsewhere
- return answerString;
- }
- console.log(fizzBuzz(15))
- //------------------------------------------------
- //Exercise: Object Oriented Programming - Car
- //------------------------------------------------
- function Car() {
- this.speed = 0;
- this.getSpeed = function () {return this.speed;}
- this.setSpeed = function (speed) {
- if (speed >= 0) {
- this.speed = speed;}
- };
- this.stop = function () {
- this.speed = 0;
- }
- }
- var myCar = new Car();
- console.log(myCar.getSpeed());
- myCar.setSpeed(10);
- console.log(myCar.getSpeed());
- myCar.stop();
- console.log(myCar.getSpeed());
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">//-------------------------------------------------
- //Name: Zach Burmaster
- //-------------------------------------------------
- //-------------------------------------------------
- //Exercise: Property Path Evaluation
- //-------------------------------------------------
- function propertyValueAt(object, array) {
- //didn't get to this, need to figure out a way to spread the number of things
- //in the array over this below, could do this by looping through and using the same
- // [array[index]] notation on the same thing over and over again
- return object[array[0]][array[1]];
- }
- var object = {a: 1, b: {c: 2, d: 3}};
- console.log(propertyValueAt(object, ['b', 'c']));
- //-------------------------------------------------
- //Exercise: Sum Nested Arrays
- //-------------------------------------------------
- function sumNested(arr) {
- //1 didn't get to checking the number of nested arrays, need to do that using the pseudocode below:
- //2 looping over the length of the array
- //3 take the first thing in the array {
- //4 if it's a number, add it to the newNumber variable {
- //5 else, do what's on line 2 again with this array
- let newArr = arr
- .flat(2)
- .reduce((accumulator, currentValue) => accumulator + currentValue)
- return newArr;
- }
- let testArr = [1, 1, 1, [3, 4, [8]], [5]];
- console.log(sumNested(testArr));;
- //-------------------------------------------------
- //Exercise: Word Count
- //-------------------------------------------------
- function wordCount(sentence) {
- var counter = 0;
- for (var i=0; i<=sentence.length; i++) {
- //feels close here, fails on string of spaces though
- if (sentence[i] == " " && (sentence[i+1] != " " || i == sentence.length)) {
- counter += 1
- }
- }
- return counter+1;
- }
- console.log(wordCount("Thisiashortsentence!"))
- //-------------------------------------------------
- //Exercise: Anagram Tester
- //-------------------------------------------------
- function areTheseAnagrams(str1, str2) {
- let arr1 = []
- for (i=0; i<str1.length; i++) {
- arr1.push(str1[i]);
- }
- let arr2= [];
- for (i=0; i<str2.length; i++) {
- arr2.push(str2[i]);
- }
- let str1Sorted = arr1.sort().join()
- let str2Sorted = arr2.sort().join()
- if (str1Sorted == str2Sorted) {
- return true;
- } else {
- return false;
- }
- }
- console.log(areTheseAnagrams("dingdong", "dongding"));
- //-------------------------------------------------
- //Exercise: Analyze Prices
- //-------------------------------------------------
- //didn't get to this one
- //------------------------------------------------
- //Exercise: Fizz Buzz
- //------------------------------------------------
- function fizzBuzz(n) {
- let answerString = ""
- for (i=0; i<=n; i++) {
- if (i%15 === 0 && i!=0) {
- answerString += i;
- answerString+='fizzbuzz, ';
- } else if (i%5 === 0 & i!=0) {
- answerString += i;
- answerString += 'buzz, ';
- } else if (i%3 === 0 & i!= 0) {
- answerString += i;
- answerString += 'fizz, ';
- } else {
- if (i !== 0) {
- answerString+= i;
- answerString+= ', ';
- }
- }
- }
- // needed to add the case on each if where i is the last number then don't put a comma in,
- // felt like my time is better spent elsewhere
- return answerString;
- }
- console.log(fizzBuzz(15))
- //------------------------------------------------
- //Exercise: Object Oriented Programming - Car
- //------------------------------------------------
- function Car() {
- this.speed = 0;
- this.getSpeed = function () {return this.speed;}
- this.setSpeed = function (speed) {
- if (speed >= 0) {
- this.speed = speed;}
- };
- this.stop = function () {
- this.speed = 0;
- }
- }
- var myCar = new Car();
- console.log(myCar.getSpeed());
- myCar.setSpeed(10);
- console.log(myCar.getSpeed());
- myCar.stop();
- console.log(myCar.getSpeed());
- </script></body>
- </html>
Add Comment
Please, Sign In to add comment