Ashley
Ashley

Reputation: 1155

Creating a For Loop in JavaScript

I am doing a project for school and I'm really struggling to understand how to create the code/commands for my for loop in JavaScript. Any help would be greatly appreciated.

The project is basically this:

To create the For Loop it says:

create a for loop that loops through each of the objects in the phrases collection. For each object in the collection do the following:

  1. Change the inner content of the second child node of the object to french[i] where i is the value of the counter variable for the for loop.
  2. Run the swapFE() function in response to the mouse-down event occurring within the object's second child node
  3. Run the swapEF() function in response to a mouse-up event occurring within the object's second child node.

Then after setting up the for loop I'm to work on the swapFE() and swapEF() functions, but right now I'm just trying to fix the for loop.

Here's what I have so far for the setUpTranslation function:

function setUpTranslation() {
   var phrases = document.getElementsByTagName("p");   
   
   for (i = 0; i< phrases.length; i++) {
       
  }

}

I have tried numerous code in the for loop but none of it seems to display the French phrase.

Upvotes: 1

Views: 327

Answers (2)

Chad Stewart
Chad Stewart

Reputation: 484

You've got a great start there. You have selected all of the

elements and assuming there are also 10, can easily walk through them all inserting your values. You can easily correlate phrases[i] with french[i] (or english[i]).

The part of the assignment that probably has you tripped up is that you need to select the second span tag. You already have the syntax down for getting all elements, so the same should apply.

phrases[i].getElementsByTagName(); // I'll let you figure out the parameter.

You can then grab the second element you find within and insert a new value.

Here is a reference to the Document Object Model (DOM), the Javascript that will help you complete your assignment. I'm sure you'll find the methods you need all by yourself. :)

http://www.javascriptkit.com/domref/

Upvotes: 1

Uri
Uri

Reputation: 89729

I haven't worked with JavaScript for many years, but what this assignment essentially asks you to do is to manipulate the DOM (the tree that represents your HTML document).

Your current tree structure is this:

Root Child node of type P First child node of type span and class pnum Second child node of type span and class phrase Another child node of type P ... ...

What your current code does is that in every iteration, you get an index from 0 to the number of child nodes of type P. You can use that to retrieve the child node of type P from the array phrases.

Now, what you are expected to do, is to use that node to obtain the second child node (the one of type phrase). There is a JavaScript operation for that. Then, you can replace the contents of that node with the contents of the string french[i]. Again, there is a JavaScript operation for that. If you do that correctly and then get to run the setup function, your document should change in place to include the French phrases after the numbers, instead of what is currently a blank area.

Because it's a homework question I can't give you the exact code you have to use (plus, it's been too long since I've written any JavaScript). However, if you look in the material you got from the course or the JavaScript reference that you got, you will see the exact operations to do each of the things I mentioned.

Upvotes: 0

Related Questions