Code Monkey home page Code Monkey logo

hs-intro-web-design-beatles-array-loops's Introduction

Looping Beatles

We've learned that arrays are really great for storing information, and we've learned how to add things to an array and how to return specific items to an array. But what if we wanted to take an action with every single item in an array? This lab is going to require that you do some learning on your own. Read through the examples below, and feel free to Google any questions you may have. If you have previous experience with loops, dive in!

###Looping

Let's take this array of numbers:

var numbers = [1, 2, 3];

And we wanted to add 5 to every number in the array and return the sum, we could do something like this:

numbers[0] + 5;
numbers[1] + 5;
numbers[2] + 5;

But what if the array was reallllllly big? Think about Twitter keeping an array of all their users. What if they had an email they wanted to send to all their users. It would take forever to trigger that email manually by using the index of the array. We can streamline this by using loops!

We're going to use a for loop to loop over an array.

for (i = 0, i < numbers.length; i++ ){
    var sum = numbers[i] + 5;
    console.log(sum);
}

In this example, we set up the variable i to be an incrementer. It's going to count up the indexes of the array. We set the for loop to run as long as the i is less than the length of the array. Then we do i++ to increment i by one every time we loop over an item in the array.

The first loop through numbers[i] is the same thing as numbers[0], which would give us the first item in the array, 1. The next time through, numbers[i] is the same thing as numbers[1], which would give us 2, and so on until there are more numbers in the array.

This code with console.log 6, 7, 8

Get To Work

You'll be coding your solution in js/beatles.js. Your job is to set up a for loop to loop over the beatles array. Each loop should console.log over the name of the Beatles, and print out "name is a Beatle". _name_ should be replaced by the names in the array.

BONUS We've learned that arrays can store all types of data (string, integers, floats, etc.). But did you know that arrays can store arrays?! It's called nested arrays:

var beatles = [["Paul McCartney", "Bass Guitar"], ["John Lennon", "Guitar"], ["George Harrison", "Lead Guitar"], ["Ringo Starr", "Drums"]]

Now, the beatles array is storing four arrays. Each nested array contains the name of a Beatle and the instrument he played.

Given this array, can you figure out how to loop over the main array, and then the nested arrays to console.log "Paul McCartney played Bass Guitar", etc. for each Beatle.

View Looping Beatles on Learn.co and start learning to code for free.

hs-intro-web-design-beatles-array-loops's People

Contributors

victhevenot avatar

Watchers

 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.