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">
- // ------------------------------------------------------------
- // Sean Gibbons
- // ------------------------------------------------------------
- // ------------------------------------------------------------
- // Exercise: Property Path Evaluation
- // ------------------------------------------------------------
- function propertyValueAt(object, propertyArray){
- if (object.hasOwnProperty(propertyArray[0])){ //if the property exists in the object
- if ((propertyArray.length > 1)){ //if there is another property to search for within this object property
- console.log(object[propertyArray[0]]);
- console.log(typeof object[propertyArray[0]]);
- if (typeof object[propertyArray[0]] === object){ //if this property is an object, and thus can hold the next property
- var nextArray = propertyArray;
- nextArray.splice(0, 1);
- propertyValueAt(object[propertyArray[0]], nextArray); //perform the function again with that object property and that next property
- }
- else{ //if this property is not an object and thus the next property cannot be within it
- return undefined;
- }
- }
- else{ //if that is the last property
- return object[propertyArray[0]]; //return that property's value
- }
- }
- else{
- return undefined; //otherwise return undefined
- }
- }
- var object1 = {
- a: 1,
- b: {
- c: 2,
- d: {
- e: 3,
- f: 4
- }
- }
- }
- console.log('propertyValueAt test');
- console.log(propertyValueAt(object1, ['a']));
- console.log(propertyValueAt(object1, ['a', 'b']));
- console.log(propertyValueAt(object1, ['b', 'c']));
- console.log(propertyValueAt(object1, ['b', 'd']));
- console.log(propertyValueAt(object1, ['b', 'd', 'e']));
- console.log(propertyValueAt(object1, []));
- // ------------------------------------------------------------
- // Exercise: Sum Nested Arrays
- // ------------------------------------------------------------
- function sumNested(array){
- var sum = 0; //sum to be returned
- for (var i = 0; i < array.length; i++){ //for each element of the array
- if (typeof array[i] === 'object'){ //if this element is an array itself
- sum += sumNested(array[i]); //add that array's sum to the sum
- }
- else{ //if this element is a number
- sum += array[i]; //add it to the sum
- }
- }
- return sum;
- }
- console.log('sumNested test')
- console.log(sumNested([]));
- console.log(sumNested([1]));
- console.log(sumNested([1, 2]));
- console.log(sumNested([1, [2, 3]]));
- console.log(sumNested([1, 1, 1, 1, [2, [3, 4]]]));
- // ------------------------------------------------------------
- // Exercise: Word Count
- // ------------------------------------------------------------
- function wordCount(sentence){
- var count = 0; //return value
- var nonSpace = new RegExp("[^\s]"); //regex for non white space
- if (nonSpace.test(sentence) === true){ //if the sentence contains characters other than white space
- var sentenceSplit = sentence.split(" "); //split the words up
- for (var i in sentenceSplit){
- count++; //for each word, increment the count
- }
- }
- else{ //if there were no words, return 0
- return 0;
- }
- if (sentence.endsWith(' ')){ //if the sentence ends with white space, decrement count so it doesn't include the empty string at the end
- return count - 1;
- }
- else{
- return count;
- }
- }
- console.log('wordCount test');
- console.log(wordCount(""));
- console.log(wordCount("a a"));
- console.log(wordCount("7vfg*"));
- console.log(wordCount("aa aa "));
- console.log(wordCount('aa aa aa'));
- // ------------------------------------------------------------
- // Exercise: Anagram Tester
- // ------------------------------------------------------------
- function areTheseAnagrams(str1, str2){
- var anagramTruth = true; //return value
- str2 = str2.split(""); //split string 2 into an array
- for (var i in str1){ //for each character in string 1
- if (str2.includes(str1[i])){ //if string 2 has that character
- charIndex = str2.indexOf(str1[i]); //find the index of the first instance of that character
- str2.splice(charIndex, 1); //remove the character so that it doesn't get counted again
- }
- else{
- anagramTruth = false;
- }
- }
- return anagramTruth;
- }
- console.log('areTheseAnagrams test')
- console.log(areTheseAnagrams("",""));
- console.log(areTheseAnagrams("a","a"));
- console.log(areTheseAnagrams("a","b"));
- console.log(areTheseAnagrams("aab","bab"));
- console.log(areTheseAnagrams("abc","cab"));
- // ------------------------------------------------------------
- // Exercise: Analyze Prices
- // ------------------------------------------------------------
- function analyzePrices(prices){
- var buyIndex = null;
- var sellIndex = null;
- var sellArray = [];
- var profit = 0; //max profit
- for (var i = 0; i < prices.length; i++){
- sellArray = prices.slice(i); //sell array is the array of numbers you can sell for after buying at a certain index
- if (sellArray[0] !== Math.min(sellArray)){ //if the buy point is not the lowest number
- if (Math.min(sellArray) - sellArray[0] > profit){ //if the profit between the buy point and the best sell point is the max profit
- profit = Math.min(sellArray) - sellArray[0]; //the max profit is changed to be correct
- buyIndex = i; //the buy index is the current buy point
- sellIndex = sellArray.indexOf(Math.min(sellArray)); //the sell index is the best sell point
- }
- }
- }
- var answer = {
- 'buyIndex': buyIndex,
- 'sellIndex': sellIndex
- }; //object to be returned
- return answer;
- }
- console.log('analyzePrices test');
- console.log(analyzePrices([1,2,3,4,5,6,7,8,9,0]));
- console.log(analyzePrices([8,4,3,1,5,5,2,8,2,3]));
- console.log(analyzePrices([5,2,5,4,5,6,3,8,1,8]));
- console.log(analyzePrices([1,1,1,1,1,1,1]));
- // ------------------------------------------------------------
- // Exercise: Fizz Buzz
- // ------------------------------------------------------------
- function fizzBuzz(n){
- if (n <= 0){
- return "";
- }
- else{
- var answer = ""; //return string
- for (i = 1; i <= n; i++){ //for numbers between 1 and n
- if ((i % 3 === 0) && (i % 5 === 0)){ //if the number is divisible by both 3 and 5
- answer += i + 'fizzbuzz'; //add the number and fizzbuzz to the screen
- }
- else if(i % 3 === 0){ //if divisible by 3
- answer += i + 'fizz';
- }
- else if(i % 5 === 0){ //if divisible by 5
- answer += i + 'buzz';
- }
- else{
- answer += i;
- }
- if (i !== n){ //if this isn't the last number
- answer += ", "; //ass a comma and a space
- }
- }
- return answer;
- }
- }
- console.log('fizzBuzz test');
- console.log(fizzBuzz(0));
- console.log(fizzBuzz(3));
- console.log(fizzBuzz(5));
- console.log(fizzBuzz(15));
- // ------------------------------------------------------------
- // Exercise: Object Oriented Programming - Car
- // ------------------------------------------------------------
- class Car {
- constructor(speed){
- if (speed !== undefined){
- this._speed = speed;
- }
- else{
- this._speed = 0;
- }
- }
- get getSpeed(){
- return this._speed;
- }
- setSpeed(speed){
- this._speed = speed;
- }
- stop(){
- this._speed = 0;
- }
- }
- console.log('Car test');
- var car = new Car();
- console.log(car.getSpeed);
- car.setSpeed(10);
- console.log(car.getSpeed);
- car.stop();
- console.log(car.getSpeed);
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">// ------------------------------------------------------------
- // Sean Gibbons
- // ------------------------------------------------------------
- // ------------------------------------------------------------
- // Exercise: Property Path Evaluation
- // ------------------------------------------------------------
- function propertyValueAt(object, propertyArray){
- if (object.hasOwnProperty(propertyArray[0])){ //if the property exists in the object
- if ((propertyArray.length > 1)){ //if there is another property to search for within this object property
- console.log(object[propertyArray[0]]);
- console.log(typeof object[propertyArray[0]]);
- if (typeof object[propertyArray[0]] === object){ //if this property is an object, and thus can hold the next property
- var nextArray = propertyArray;
- nextArray.splice(0, 1);
- propertyValueAt(object[propertyArray[0]], nextArray); //perform the function again with that object property and that next property
- }
- else{ //if this property is not an object and thus the next property cannot be within it
- return undefined;
- }
- }
- else{ //if that is the last property
- return object[propertyArray[0]]; //return that property's value
- }
- }
- else{
- return undefined; //otherwise return undefined
- }
- }
- var object1 = {
- a: 1,
- b: {
- c: 2,
- d: {
- e: 3,
- f: 4
- }
- }
- }
- console.log('propertyValueAt test');
- console.log(propertyValueAt(object1, ['a']));
- console.log(propertyValueAt(object1, ['a', 'b']));
- console.log(propertyValueAt(object1, ['b', 'c']));
- console.log(propertyValueAt(object1, ['b', 'd']));
- console.log(propertyValueAt(object1, ['b', 'd', 'e']));
- console.log(propertyValueAt(object1, []));
- // ------------------------------------------------------------
- // Exercise: Sum Nested Arrays
- // ------------------------------------------------------------
- function sumNested(array){
- var sum = 0; //sum to be returned
- for (var i = 0; i < array.length; i++){ //for each element of the array
- if (typeof array[i] === 'object'){ //if this element is an array itself
- sum += sumNested(array[i]); //add that array's sum to the sum
- }
- else{ //if this element is a number
- sum += array[i]; //add it to the sum
- }
- }
- return sum;
- }
- console.log('sumNested test')
- console.log(sumNested([]));
- console.log(sumNested([1]));
- console.log(sumNested([1, 2]));
- console.log(sumNested([1, [2, 3]]));
- console.log(sumNested([1, 1, 1, 1, [2, [3, 4]]]));
- // ------------------------------------------------------------
- // Exercise: Word Count
- // ------------------------------------------------------------
- function wordCount(sentence){
- var count = 0; //return value
- var nonSpace = new RegExp("[^\s]"); //regex for non white space
- if (nonSpace.test(sentence) === true){ //if the sentence contains characters other than white space
- var sentenceSplit = sentence.split(" "); //split the words up
- for (var i in sentenceSplit){
- count++; //for each word, increment the count
- }
- }
- else{ //if there were no words, return 0
- return 0;
- }
- if (sentence.endsWith(' ')){ //if the sentence ends with white space, decrement count so it doesn't include the empty string at the end
- return count - 1;
- }
- else{
- return count;
- }
- }
- console.log('wordCount test');
- console.log(wordCount(""));
- console.log(wordCount("a a"));
- console.log(wordCount("7vfg*"));
- console.log(wordCount("aa aa "));
- console.log(wordCount('aa aa aa'));
- // ------------------------------------------------------------
- // Exercise: Anagram Tester
- // ------------------------------------------------------------
- function areTheseAnagrams(str1, str2){
- var anagramTruth = true; //return value
- str2 = str2.split(""); //split string 2 into an array
- for (var i in str1){ //for each character in string 1
- if (str2.includes(str1[i])){ //if string 2 has that character
- charIndex = str2.indexOf(str1[i]); //find the index of the first instance of that character
- str2.splice(charIndex, 1); //remove the character so that it doesn't get counted again
- }
- else{
- anagramTruth = false;
- }
- }
- return anagramTruth;
- }
- console.log('areTheseAnagrams test')
- console.log(areTheseAnagrams("",""));
- console.log(areTheseAnagrams("a","a"));
- console.log(areTheseAnagrams("a","b"));
- console.log(areTheseAnagrams("aab","bab"));
- console.log(areTheseAnagrams("abc","cab"));
- // ------------------------------------------------------------
- // Exercise: Analyze Prices
- // ------------------------------------------------------------
- function analyzePrices(prices){
- var buyIndex = null;
- var sellIndex = null;
- var sellArray = [];
- var profit = 0; //max profit
- for (var i = 0; i < prices.length; i++){
- sellArray = prices.slice(i); //sell array is the array of numbers you can sell for after buying at a certain index
- if (sellArray[0] !== Math.min(sellArray)){ //if the buy point is not the lowest number
- if (Math.min(sellArray) - sellArray[0] > profit){ //if the profit between the buy point and the best sell point is the max profit
- profit = Math.min(sellArray) - sellArray[0]; //the max profit is changed to be correct
- buyIndex = i; //the buy index is the current buy point
- sellIndex = sellArray.indexOf(Math.min(sellArray)); //the sell index is the best sell point
- }
- }
- }
- var answer = {
- 'buyIndex': buyIndex,
- 'sellIndex': sellIndex
- }; //object to be returned
- return answer;
- }
- console.log('analyzePrices test');
- console.log(analyzePrices([1,2,3,4,5,6,7,8,9,0]));
- console.log(analyzePrices([8,4,3,1,5,5,2,8,2,3]));
- console.log(analyzePrices([5,2,5,4,5,6,3,8,1,8]));
- console.log(analyzePrices([1,1,1,1,1,1,1]));
- // ------------------------------------------------------------
- // Exercise: Fizz Buzz
- // ------------------------------------------------------------
- function fizzBuzz(n){
- if (n <= 0){
- return "";
- }
- else{
- var answer = ""; //return string
- for (i = 1; i <= n; i++){ //for numbers between 1 and n
- if ((i % 3 === 0) && (i % 5 === 0)){ //if the number is divisible by both 3 and 5
- answer += i + 'fizzbuzz'; //add the number and fizzbuzz to the screen
- }
- else if(i % 3 === 0){ //if divisible by 3
- answer += i + 'fizz';
- }
- else if(i % 5 === 0){ //if divisible by 5
- answer += i + 'buzz';
- }
- else{
- answer += i;
- }
- if (i !== n){ //if this isn't the last number
- answer += ", "; //ass a comma and a space
- }
- }
- return answer;
- }
- }
- console.log('fizzBuzz test');
- console.log(fizzBuzz(0));
- console.log(fizzBuzz(3));
- console.log(fizzBuzz(5));
- console.log(fizzBuzz(15));
- // ------------------------------------------------------------
- // Exercise: Object Oriented Programming - Car
- // ------------------------------------------------------------
- class Car {
- constructor(speed){
- if (speed !== undefined){
- this._speed = speed;
- }
- else{
- this._speed = 0;
- }
- }
- get getSpeed(){
- return this._speed;
- }
- setSpeed(speed){
- this._speed = speed;
- }
- stop(){
- this._speed = 0;
- }
- }
- console.log('Car test');
- var car = new Car();
- console.log(car.getSpeed);
- car.setSpeed(10);
- console.log(car.getSpeed);
- car.stop();
- console.log(car.getSpeed);
- </script></body>
- </html>
Add Comment
Please, Sign In to add comment