Skip to content

Latest commit

 

History

History
160 lines (125 loc) · 5.19 KB

File metadata and controls

160 lines (125 loc) · 5.19 KB

week0.day1.arrays

Array basics

1. Mastering syntax

  • If you haven't already done so, create a file called arrays.js
  • In this file, define an array called grades that contains the values: 93, 100, 65, 84, 80
  • Print out the 2nd and 4th elements in grades (Remember, an item's index is not the same as it's position)
  • Execute your JavaScript code using node arrays.js on the command line to see that everything is working correctly
  • Use this file to test your answers the following questions
    • If you want to remove code, try commenting it out instead
    // double backslash removes code from execution, but keeps it in the document!

2. Printing an array

Given the following array:

const coins = [1, 5, 10, 25, 50];

Write code that prints out each element of the array on a separate line (do not use a loop, we'll get there soon!).

3. Modifying an array

Given the following array:

const lunchSpecials = [5, 10, 6, 7];

Write code that reassigns each item to itself multiplied by 2. After you're done, print out each value of the array to make sure that you've done this correctly.

Array transformations

Write code to transform the given array into the "AFTER" state (see example below)

  • Do not reassign the entire array (you can't since it is declared const).
  • Do not use loops

FOR EXAMPLE: Transform list into the "AFTER" state:

const list = [1, 2, 3, 4]; 
//    AFTER: [0, 0, 0, 0]

One solution:

list[0] = 0;
list[1] = 0;
list[2] = 0;
list[3] = 0;

1. Transform using the push method.

const list = [1, 2, 3, 4]; 
//    AFTER: [1, 2, 3, 4, 5]

2. Transform using the pop method.

const list = [1, 2, 3, 4, 5]; 
//    AFTER: [1, 2, 3]

3. Transform using the unshift method.

const list = [1, 2, 3, 4, 5]; 
//    AFTER: [7, 6, 1, 2, 3, 4, 5];

4. Transform using the reverse method.

const list = [1, 2, 3, 4, 5]; 
//    AFTER: [5, 4, 3, 2, 1];

5. Transform using the splice method.

const list = [1, 2, 3, 4, 5]; 
//    AFTER: [1, 2, 3, 99, 5];

Array Info

let items = ["Boots of Speed", "Health Potion", "Doran's Blade", "Warding Totem"];

Using the above array, gather the following information without using inefficient human tactics. Why? This array changes often! Items are taken away and added, so your code must be able to handle that - see the link for help:

  1. What is the length of the array?
  2. Is there a "Boots of Speed" in the array?
  3. Is there a "Lich Bane" in the array?
  4. What's the index of the "Health Potion" in the array?

Array Info (20 minutes later)

console.log(items);
// ["Ionian Boots of Lucidity", "Infinity Edge", "Lich Bane", "Luden's Echo","Spirit Visage"];

Go figure, the array has changed! The code you made from before should still answer the above questions correctly without any changes!

  1. Make sure the code from before works correctly with this new array
  2. Don't ask what kind of build this is, because I don't know

Spot the error

Pretend to be the computer for a moment and find what the error is before running it. Note: some things may be of poor design, but not technically cause an error

  1.  cosnt myArray = [2,4,5,7];
  2.  const myArray = [
       "this",
       "is",
       "correct",
     ]
  3.  const list = [1,3,4,5,7,8];
     list[1] = 5;
     list = list.reverse();
  4.  const "myList" = ["something","more"];
  5.  let tic_tac_toe = [
       [O,O,X],
       [O,X,X],
       [O,X,O]
     ];

Modification through .length

const x = [4,5,6,7,4,1,1];
x.length; // 7
x.length = 3;
x.length; // 3
console.log(x);

Try this out. What happened to the array?

Look over this worksheet to help fill in any gaps. We’ll be reviewing these on Monday!