Return to Video

For-In Loops Quiz - JavaScript Basics

  • 0:00 - 0:04
    So here's one last helpful loop, the for in loop.
  • 0:04 - 0:08
    For in loops let us easily iterate through arrays or objects.
  • 0:08 - 0:09
    The syntax looks like this.
  • 0:09 - 0:11
    The same keyword for.
  • 0:11 - 0:14
    We have some kind of iterator, which we can give any name that we want.
  • 0:14 - 0:17
    Then we have the keyword in, and then an object or
  • 0:17 - 0:19
    array that we want to iterate through.
  • 0:19 - 0:22
    For each of the items in some object, we want to do something.
  • 0:22 - 0:27
    Just like before, we put the conditions for our for loop inside parentheses and
  • 0:27 - 0:30
    the statements we want to execute inside curly braces.
  • 0:30 - 0:32
    It's important to note here that this item,
  • 0:32 - 0:36
    this iterator in this list is actually the index of the item in the object,
  • 0:36 - 0:40
    not the value held at the index in the object.
  • 0:40 - 0:41
    So let's take a look at an example.
  • 0:41 - 0:43
    We have an array called countries,
  • 0:43 - 0:46
    which has the top four teams of the 2014 World Cup.
  • 0:46 - 0:49
    We have a for in loop here, which is going to iterate for
  • 0:49 - 0:53
    each country in the countries array, we want to log that country.
  • 0:53 - 0:54
    Hey, James.
  • 0:54 - 0:56
    Do you see anything wrong with this code?
  • 0:56 - 0:59
    >> It's just going to print out, zero through three and
  • 0:59 - 1:01
    not the actual country name.
  • 1:01 - 1:01
    >> Why is that?
  • 1:01 - 1:04
    >> Because country is an iterator in countries.
  • 1:04 - 1:06
    It's not the actual value of country.
  • 1:06 - 1:08
    >> That's right, so let's make a change.
  • 1:08 - 1:12
    Instead we should have console.log countries of the index country.
  • 1:12 - 1:13
    Let's see if this is correct.
  • 1:13 - 1:16
    So, here's our for in loop in one line of code.
  • 1:16 - 1:19
    By using Shift + Enter, we can actually put this in a much nicer format.
  • 1:19 - 1:24
    So we see, for (country in countries) { console.log(countries[country]) }; and
  • 1:24 - 1:25
    I'm pressing Enter.
  • 1:25 - 1:28
    We see Germany, Argentina, Brazil, and The Netherlands.
  • 1:28 - 1:32
    >> You should already have a work object with all of your work experiences.
  • 1:32 - 1:36
    I want you to write a for-in loop that iterates over all the jobs in
  • 1:36 - 1:41
    your work object and appends them in a new HTMLworkStart element.
  • 1:41 - 1:46
    I want you to also format each job's employer with HTMLworkEmployer in
  • 1:46 - 1:52
    each job title with HTMLworkTitle and append all of that to the page.
  • 1:52 - 1:53
    Click this box when you're done.
タイトル:
For-In Loops Quiz - JavaScript Basics
概説:

more » « less
Video Language:
English
Team:
Udacity
プロジェクト:
ud804 - Javascript Basics
Duration:
01:54

English subtitles

改訂 Compare revisions