Git Product home page Git Product logo

cssi-2-javascript-arrays-practice's Introduction

JavaScript Arrays Practice

##Instructions:

###1. Arrays:

Arrays are lists of elements, similar to a bookshelf that has slots to store different books (the elements) in a single shelf. Books can be fetched by identifying their position in the shelf (index).

  • To declare an empty array, we can use the following construct:
var myArr = [];
  • If we know already what elements we want to store in the array, we can put them inside the brackets:
var myArr2 = [element, anotherElement];

Elements can be strings, numbers, boolean, or even undefined. If you leave a blank spot in an array it creates a blank shelf space (undefined) placeholder. Keeping these characteristics in mind, make a new array, myArr, with four elements that will behave in the following ways:

myArr[0]
  --> 'Hello'
myArr[1]
  --> undefined
myArr[2]
  --> 54.3
myArr[3]
  --> true
  • We can insert new values into any space in the array using the positions index. Change the undefined item in myArr to equal the string "Stuff" instead.

###2. Overwriting: We can overwrite all the elements of an array by assigning a new value to each of them. Or if we have two arrays, we can replace all elements of the first with those of the second in one statement, by assigning one array to the other.

  • Make an array called fruits where the four elements are these strings: Apples, Oranges, Pears, and Bananas.
  • Overwrite myArr so that its elements are the same as in fruits.

###3. Retrieving the Total Number of Elements and Last Index:

  • Using the property length of myArr. Is the result the number you were expecting? Why? (Note how length is a property, not a method like those that we have seen for strings. Can you spot the difference?)
  • Keeping in mind the fact that indexes start with zero instead of one, get the last element's index position in myArr.
  • Make a variable "pos" and set it equal to the number you found in the step just above.
  • What should myArr[pos] return? Does it?

###4. Array Insertion and Deletion: We can insert on to the end of an Array simply by using the push() method.

  • Insert the string "Strawberries" into "myArr".
  • You can pull the last element off the end using the pop() method.
  • Call pop on "myArr". What will myArr[myArr.length - 1] return? Does it?
  • We can use splice method to insert content at a given position or to remove content from a given position. Here is the syntax:
arrayToModify.splice(index, how many elements to remove, optional content to add);
  • Add the string "Tiger" at index 2 without removing any elements. What do you think myArr.length; will return?
  • Let's say we needed to remove the second to last item, but we do not know how long the array is...We can use negative numbers to go to end of array and index from end to beginning.
  • Replace "Pears" with "Lion" using the negative index number, using the info above.
  • Now remove Lion from the 3rd index position using splice.

View JavaScript Arrays Practice on Learn.co and start learning to code for free.

cssi-2-javascript-arrays-practice's People

Contributors

crepric avatar fislabstest avatar nanselmo avatar sarogers avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.