In-Class exercise: JavaScript

JavaScript

  1. Write a function countX that counts the number of x characters in a string. For example,
    countX("abcxxxbcdxx") 
    returns
    5
    
  2. Write a function wordCount that takes a string and returns an object with the words as properties and the values as the word count of those words.

    For this exercise, you may assume that words are separated by single spaces.

    For example,

    wordCount("This is a test of whether the test is a good test") 
    returns 
    { This: 1, is: 2, a: 2, test: 3, of: 1, whether: 1, the: 1, good: 1 }
    
  3. Given the following class:
    class Foo {
      constructor(callback) {
        this.callback = callback;
      }
    
      doSomething(value){
        this.callback(value + 5);
      }
    }
    
    Initialize f to be an object of class Foo. When you call:
    f.doSomething(3);
    
    it should print 8 to the console.
  4. Given the declaration of Foo above, create a class Bar with a constructor and doSomething method:
    class Bar extends Foo {
      constructor(callback, multiplier) {
        should call Foo's constructor and save multiplier
      }
    
      doSomething(value) {
        Should return Foo's doSomething(value * saved multiplier)
      }
    }
    
    Initialize b to be an object of class Bar with multiplier 3. When you call:
    b.doSomething(3);
    
    it should print 14 to the console.

Hints