Javascript Exercises

Section 1 - Basic Syntax

Write a function for each of the following and run it in JS. Use console.log for output:

  1. Randomizes three numbers in range (0.. 100) and prints the largest one.
  2. Randomizes a number (range 0..100), then prints all the even numbers from 0 to the randomized.
  3. Randomizes a number (range 0..100), then prints all the odd numbers from 40 to that one. If the number was smaller than 40 nothing should be printed
  4. Randomizes a number (range 0..100), then prints all the odd numbers from 40 to that one. If the number was smaller than 40, print all the numbers down to the randomized one (i.e. if the result was 37, you should print: 40, 39, 38, 37)
  5. Randomizes a number n in range 0..100. Now randomizes n more numbers in that range, printing the largest of them.
  6. Randomizes a number (range 1000 .. 9999 ) and calculate the sum of its digits. For instance, if the randomized number was 1049, program should print 14
  7. Randomizes a number (range 1000..9999) and calculate the sum of its digits repeatedly until you reach one digit only. For instance, if the randomized number was 1049, program should print 5
  8. Randomizes two numbers and prints the smallest number that is greater than one, such that both numbers are divided by it with no remainder. For instance, for 9 and 6 you should print 3. If there isn't one a proper note should be printed.
  9. Randomizes two numbers and prints their least common multiplication of them. (use http://en.wikipedia.org/wiki/Least_common_multiple for details)
  10. Randomizes two numbers and prints their average and standard deviation.

Section 2 - Objects and Arrays

Fill in the blanks to make the program work:

(function() {
    
    var larger = function(o1, o2) {
        if ( o1.size > o2.size ) {
            console.log('o1 is larger');
        } else {
            console.log('o2 is larger');
        }
    };
    
    var x = // write code here
    var y = // write code here
    
    larger(x, y);    
    
}());
(function() {

    var favorite_album = function( collection ) {
        // do nothing if got an empty collection
        if ( collection.length === 0 ) {
            return;
        }

        // define two variables and initialize them
        var max_played = collection[0].played,
            max_index  = 0;

        for ( var i=0, len = collection.length; i < len; i++ ) {
            if ( collection[i].played > max_played ) {
                max_played = collection[i].played;
                max_index  = i;
            }
        }

        return { play: max_played, index: max_index };
    };

    var music = // write code here

    var fav = favorite_album( music );

    console.log( "Your favorite album was played " + fav.play + " times" );

    // Bonus: Write code here to make the following line print the above line
    console.log( fav );
}());


Implement a Summer objects with two methods: getCurrentSum() and add(number). Run the following main code to make sure everything works:

(function () {
  // write your code here to make the program work 

  var s1 = Summer();
  var s2 = Summer();
 
  s1.add(10);
  s1.add(20);
 
  s2.add(30);
 
  s2.add(5);
 
  // prints 30
  console.log(s1.getCurrentSum());
 
  // prints 35
  console.log(s2.getCurrentSum());
 
}());

Objects 1

(function () {
  
  var person = {
    // fill code here
  };

  var  car = {
    // fill code here
  };

  // print Vrum Vrum
  car.drive();
 
  // print I'm rich
  if ( car.price > 1000 ) {
    person.buy(car);
  }
}());
(function () {
  
  var employees = // fill code here;

  for (var i=0; i < employees.length; ++i ) {
    // print I work at IBM
    employees[i].hello();
  }

}());

Implement the following Bingo object. The Bingo object should randomize a number between 1 and 10 on creation and provide a single method guess(number). That method should return true if the argument was the secret number. Test your class.

Implement a Music Box object that should make the following code work well:

(function() {
// Write your code here  

  var box = MusicBox(),
      a1 = Album("The Who", "Tommy"),
      a2 = Album("Tom Waits", "Closing Time"),
      a3 = Album("John Cale", "Paris 1919"),
      favorite;
 
  box.addAlbum(a1);
  box.addAlbum(a2);
  box.addAlbum(a3);
 
  a1.play() ; // prints "Playing The Who - Tommy"
  a2.play(); // prints "Playing Tom Waits - Closing Time"  
  a1.play(); // prints "Playing The Who - Tommy"
 
  favorite = box.favoriteAlbum(); 
 
  // prints "favorite album is The Who - Tommy"
  console.log("favorite album is " + favorite); 
 
}());

Can you also write it so that box.addAlbum(a1, a2, a3) will work ? how ?

Write the functions that will make the following code work:

(function () {
  // Code goes here

  var album = PhotoAlbum(),
      p;
 
  p = Photo("Paris Trip 1");
  p.tag("Jimmy");
  p.tag("Jane");
  p.tag("Jeff");
 
  album.addPicture(p);
 
  p = Photo("Look the Eiffel");
  p.tag("Jimmy");
  p.tag("Max");
  album.addPicture(p);
 
  p = Photo("OMG it's so high");
  p.tag("Jimmy");
  p.tag("Jane");
 
  // prints "Jimmy, Jane"
  p.showTags();
 
  album.addPicture(p);
 
  // prints "Paris Trip 1, Look the Eiffel, OMG it's so high"
  album.showPictures("Jimmy");
 
  // prints "Paris Trip 1, OMG it's so high"
  album.showPictures("Jane");    
}());

Section 3 - Objects Relations

Create a line object that can "draw" a line by typing a line of '*' to the console

Link To Solutions