Exercise Solutions: ArraysΒΆ

  1. Create an array called practiceFile with the following entry: 273.15. Use the push method to add the following elements to the array. Add items a & b one at a time, then use a single push to add the items in part c. Print the array after each step to confirm the changes.

    let practiceFile = [273.15];
    
    1. 42

      1
      2
      practiceFile.push(42);
      console.log(practiceFile);
      
    1. false, -4.6, "87"

      1
      2
      practiceFile.push(false, -4.6, "87");
      console.log(practiceFile);
      

Back to the exercises.

  1. push, pop, shift and unshift are used to add/remove elements from the beginning/end of an array. Bracket notation can be used to modify any element within an array. Starting with the cargoHold array ['oxygen tanks', 'space suits', 'parrot', 'instruction manual', 'meal packs', 'slinky', 'security blanket'], write statements to do the following:

    let cargoHold = ['oxygen tanks', 'space suits', 'parrot', 'instruction manual', 'meal packs', 'slinky', 'security blanket'];
    
    1. Use bracket notation to replace 'slinky' in the array with 'space tether'. Print the array to confirm the change.

      1
      2
      cargoHold[5] = 'space tether';
      console.log(cargoHold);
      
    1. Remove the first item from the array with shift. Print the element removed and the updated array.

      1
      2
      console.log(cargoHold.shift());
      console.log(cargoHold);
      
    1. Use a template literal to print the final array and its length.

      console.log(`The array ${cargoHold} has a length of ${cargoHold.length}.`);
      

Back to the exercises.

  1. The splice method can be used to either add or remove items from an array. It can also accomplish both tasks at the same time. Review the splice appendix if you need a syntax reminder. Use splice to make the following changes to the final cargoHold array from exercise 2. Be sure to print the array after each step to confirm your updates.

    1. Insert the string 'keys' at index 3 without replacing any other entries.

      1
      2
      cargoHold.splice(3,0,'keys');
      console.log(cargoHold);
      
    1. Replace the elements at indexes 2 - 4 with the items 'cat', 'fob', and 'string cheese'.

      1
      2
      cargoHold.splice(2,3,'cat','fob','string cheese');
      console.log(cargoHold);
      

Back to the exercises.

  1. Some methods---like splice and push---alter the original array, while others do not. Use the arrays

    holdCabinet1 ['duct tape', 'gum', 3.14, false, 6.022e23]
    

    and

    holdCabinet2 ['orange drink', 'nerf toys', 'camera', 42, 'parsnip']
    

    to explore the following methods: concat, slice, reverse, sort. Refer back to the chapter if you need to review the proper syntax for any of these methods.

    1. Print the result of using concat on the two arrays. Does concat alter the original arrays? Verify this by printing holdCabinet1 after using the method.

      1
      2
      console.log(holdCabinet1.concat(holdCabinet2));
      console.log(holdCabinet1);
      
    1. reverse the first array, and sort the second. What is the difference between these two methods? Do the methods alter the original arrays?

      1
      2
      3
      4
      holdCabinet1.reverse();
      holdCabinet2.sort();
      console.log(holdCabinet1);
      console.log(holdCabinet2);
      

Back to the exercises.

  1. The split method converts a string into an array, while the join method does the opposite.

    1. Try it! Given the string str = 'In space, no one can hear you code.', see what happens when you print str.split() vs. str.split('e') vs. str.split(' ') vs. str.split(''). What is the purpose of the parameter inside the ()?

      1
      2
      3
      4
      console.log(str.split());
      console.log(str.split('e'));
      console.log(str.split(' '));
      console.log(str.split(''));
      
    1. Do split or join change the original string/array?

      console.log(cargoHold.split(',').sort().join(','));
      

Back to the exercises.

  1. Arrays can hold different data types, even other arrays! A multi-dimensional array is one with entries that are themselves arrays.

    1. Define and initialize the following arrays, which hold the name, chemical symbol and mass for different elements:

      1. element1 = ['hydrogen', 'H', 1.008]

      2. element2 = ['helium', 'He', 4.003]

      3. element26 = ['iron', 'Fe', 55.85]

      1
      2
      3
      let element1 = ['hydrogen', 'H', 1.008];
      let element2 = ['helium', 'He', 4.003];
      let element26 = ['iron', 'Fe', 55.85];
      
    1. Use bracket notation to examine the difference between printing table[1] and table[1][1]. Don't just nod your head! I want to HEAR you describe this difference. Go ahead, talk to your screen.

      console.log(table[1], table[1][1]);
      

Back to the exercises.