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">
- //----------------------------------------------------
- //Paige Williams
- //----------------------------------------------------
- //----------------------------------------------------
- //Exercise: Property Path Evaluation
- //----------------------------------------------------
- /*
- //loop through array and print values
- function propertyValueAt(object, arr) {
- return Object.values(object)
- }
- var object = {
- a: 1,
- b: {
- c: 2,
- d: 3
- }
- };
- console.log(propertyValueAt(object, ['a']));
- console.log(propertyValueAt(object, ['b']));
- console.log(propertyValueAt(object, ['c']));
- console.log(propertyValueAt(object, ['d']));
- console.log(propertyValueAt(object, ['z']));
- */
- //----------------------------------------------------
- //Exercise: Sum Nested Arrays
- //----------------------------------------------------
- /*
- //loops through array and any arrays within the array
- // adding each number as it goes
- function sumNested(arr) {
- var sum = 0;
- for (var i = 0; i < arr.length; i++) {
- for (var j = 0; j < arr[i].length; j++) {
- sum += arr[i][j]
- }
- }
- return sum;
- }
- console.log(sumNested([1, 1, 1, [3, 4, [8]], [5]]));
- console.log(sumNested([]))
- */
- //----------------------------------------------------
- //Exercise: Word Count
- //----------------------------------------------------
- /*
- //loops through the string checking for spaces, counts spaces in between words and
- // returns the final count + 1 to add in the first word
- function wordCount(sentence) {
- var count = 0;
- for (var i = 0; i < sentence.length; i++) {
- if (sentence[i] === ' ') {
- count++;
- }
- }
- return count + 1;
- }
- console.log(wordCount('This is a short sentence!'));
- console.log(wordCount('ThisIsA!$LongWord!'));
- console.log(wordCount(' ')); //doesn't return correctly due to counting of the spaces
- */
- //----------------------------------------------------
- //Exercise: Anagram Tester
- //----------------------------------------------------
- /*
- //sorts the strings in alaphebetical order, then checks if they are equal
- function areTheseAnagrams(str1, str2) {
- var arr1 = str1.split('');
- var sorted1 = arr1.sort();
- var arr2 = str2.split('');
- var sorted2 = arr2.sort();
- if (sorted1.join('') == sorted2.join('')) {
- return true;
- } else {
- return false;
- }
- }
- (console.log(areTheseAnagrams('abc', 'bac')));
- (console.log(areTheseAnagrams('abc', 'cde')));
- */
- //----------------------------------------------------
- //Exercise: Analyze Prices
- //----------------------------------------------------
- /*
- function analyzePrices(prices) {
- var test;
- var maxProfit = -500;
- var buyIndex;
- var sellIndex;
- for (var i = 0; i < prices.length; i++) {
- for (var j = 0; j < prices.length; j++) {
- if (i < j) {
- test = prices[j] - prices[i]
- if (test > maxProfit) {
- maxProfit = test;
- buyIndex = i;
- sellIndex = j;
- }
- }
- }
- }
- console.log('Max Profit ' + maxProfit + ' ' +
- '{ buy index: ' + buyIndex + ', ' + sellIndex + '}');
- }
- analyzePrices([1,2,3,4,5,6,7,8,9,0]);
- analyzePrices([8,4,3,1,5,5,2,8,2,3]);
- */
- //----------------------------------------------------
- //Exercise: Fizz Buzz
- //----------------------------------------------------
- /*
- //prints numbers from 1 to n, checks if the remainder of i / 3, i / 5, = 0,
- // if so, prints fizz and buzz accordingly
- function fizzBuzz(n) {
- if (n <= 0) {
- return '';
- }
- for (var i = 1; i < n; i++) {
- if ((i % 3 === 0) && (i & 5 === 0)) {
- console.log(i + 'fizzbuzz');
- } else if (i % 3 === 0) {
- console.log(i + 'fizz');
- } else if (i % 5 === 0) {
- console.log(i + 'buzz');
- } else {
- console.log(i);
- }
- }
- }
- console.log(fizzBuzz(100));
- console.log(fizzBuzz(-5));
- */
- //----------------------------------------------------
- //Exercise: Car
- //----------------------------------------------------
- /*
- class Car {
- constructor() {
- this._speed = 0;
- }
- getSpeed() {
- return this._speed;
- }
- setSpeed(speed) {
- this._speed = speed;
- }
- stop() {
- this._speed = 0;
- }
- }
- 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">//----------------------------------------------------
- //Paige Williams
- //----------------------------------------------------
- //----------------------------------------------------
- //Exercise: Property Path Evaluation
- //----------------------------------------------------
- /*
- //loop through array and print values
- function propertyValueAt(object, arr) {
- return Object.values(object)
- }
- var object = {
- a: 1,
- b: {
- c: 2,
- d: 3
- }
- };
- console.log(propertyValueAt(object, ['a']));
- console.log(propertyValueAt(object, ['b']));
- console.log(propertyValueAt(object, ['c']));
- console.log(propertyValueAt(object, ['d']));
- console.log(propertyValueAt(object, ['z']));
- */
- //----------------------------------------------------
- //Exercise: Sum Nested Arrays
- //----------------------------------------------------
- /*
- //loops through array and any arrays within the array
- // adding each number as it goes
- function sumNested(arr) {
- var sum = 0;
- for (var i = 0; i < arr.length; i++) {
- for (var j = 0; j < arr[i].length; j++) {
- sum += arr[i][j]
- }
- }
- return sum;
- }
- console.log(sumNested([1, 1, 1, [3, 4, [8]], [5]]));
- console.log(sumNested([]))
- */
- //----------------------------------------------------
- //Exercise: Word Count
- //----------------------------------------------------
- /*
- //loops through the string checking for spaces, counts spaces in between words and
- // returns the final count + 1 to add in the first word
- function wordCount(sentence) {
- var count = 0;
- for (var i = 0; i < sentence.length; i++) {
- if (sentence[i] === ' ') {
- count++;
- }
- }
- return count + 1;
- }
- console.log(wordCount('This is a short sentence!'));
- console.log(wordCount('ThisIsA!$LongWord!'));
- console.log(wordCount(' ')); //doesn't return correctly due to counting of the spaces
- */
- //----------------------------------------------------
- //Exercise: Anagram Tester
- //----------------------------------------------------
- /*
- //sorts the strings in alaphebetical order, then checks if they are equal
- function areTheseAnagrams(str1, str2) {
- var arr1 = str1.split('');
- var sorted1 = arr1.sort();
- var arr2 = str2.split('');
- var sorted2 = arr2.sort();
- if (sorted1.join('') == sorted2.join('')) {
- return true;
- } else {
- return false;
- }
- }
- (console.log(areTheseAnagrams('abc', 'bac')));
- (console.log(areTheseAnagrams('abc', 'cde')));
- */
- //----------------------------------------------------
- //Exercise: Analyze Prices
- //----------------------------------------------------
- /*
- function analyzePrices(prices) {
- var test;
- var maxProfit = -500;
- var buyIndex;
- var sellIndex;
- for (var i = 0; i < prices.length; i++) {
- for (var j = 0; j < prices.length; j++) {
- if (i < j) {
- test = prices[j] - prices[i]
- if (test > maxProfit) {
- maxProfit = test;
- buyIndex = i;
- sellIndex = j;
- }
- }
- }
- }
- console.log('Max Profit ' + maxProfit + ' ' +
- '{ buy index: ' + buyIndex + ', ' + sellIndex + '}');
- }
- analyzePrices([1,2,3,4,5,6,7,8,9,0]);
- analyzePrices([8,4,3,1,5,5,2,8,2,3]);
- */
- //----------------------------------------------------
- //Exercise: Fizz Buzz
- //----------------------------------------------------
- /*
- //prints numbers from 1 to n, checks if the remainder of i / 3, i / 5, = 0,
- // if so, prints fizz and buzz accordingly
- function fizzBuzz(n) {
- if (n <= 0) {
- return '';
- }
- for (var i = 1; i < n; i++) {
- if ((i % 3 === 0) && (i & 5 === 0)) {
- console.log(i + 'fizzbuzz');
- } else if (i % 3 === 0) {
- console.log(i + 'fizz');
- } else if (i % 5 === 0) {
- console.log(i + 'buzz');
- } else {
- console.log(i);
- }
- }
- }
- console.log(fizzBuzz(100));
- console.log(fizzBuzz(-5));
- */
- //----------------------------------------------------
- //Exercise: Car
- //----------------------------------------------------
- /*
- class Car {
- constructor() {
- this._speed = 0;
- }
- getSpeed() {
- return this._speed;
- }
- setSpeed(speed) {
- this._speed = speed;
- }
- stop() {
- this._speed = 0;
- }
- }
- var car = new Car();
- console.log(car.getSpeed());
- car.setSpeed(10);
- console.log(car.getSpeed());
- car.stop();
- console.log(car.getSpeed());
- */
- </script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement