Wrox Programmer Forums
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 4th, 2004, 10:10 AM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default What is "i"

Greetings,

I use Dreamweaver for my code and sometimes what I do is use some of their behaviors and then modify them to suit my needs. Using one of their behaviors generated the following code...

function goNext() {
  var currPage, i
  for (i=0; i<=(quiz.length-1); i++) {
    if (parent.main.location.href.indexOf(quiz[i].src) != -1) {
      parent.main.location.href = quiz[i+1].src
      break
    }

My question is this...what does the "i" do/mean here? Can anyone enlighten me?

Clay Hess
__________________
Clay Hess
 
Old June 4th, 2004, 10:28 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

The i in this example is an index variable used inside the loop.

This code loops while i is less than or equals to the number of quiz items. It does this by giving i a starting value of 0. Then the code between the brackets will run. On the next iteration, i is incremented by one (i++). If i is still less than or equal to quiz.length-1, the code will continue.

So, imagine the quiz had 4 questions, the code would loop for i = 0, 1, 2 and 3.

Inside the loop, you can then use i to retrieve a specific item, usually related to the value of i, as in this example:

quiz[i+1].src

In this example, the ith + 1 item is retrieved from the array with quiz questions.

Does this help?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old June 4th, 2004, 10:35 AM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

THanks...yes that does make sense. Now maybe you can answer another question for me. Here is the quiz array...

var quiz = new Array()

quiz[0] = new quizPage("al_home.htm")
quiz[1] = new quizPage("al_q1.htm")
quiz[2] = new quizPage("al_q2.htm")
quiz[3] = new quizPage("al_q3.htm")
quiz[4] = new quizPage("al_q4.htm")
quiz[5] = new quizPage("al_q5.htm")
quiz[6] = new quizPage("al_q6.htm")
quiz[7] = new quizPage("al_q7.htm")
quiz = new quizPage("al_q8.htm")
quiz[9] = new quizPage("al_q9.htm")
quiz[10] = new quizPage("al_q10.htm")
quiz[11] = new quizPage("al_sum.htm")
quiz[12] = new quizPage("al_home.htm")

Now looking at this array and the code in my previous post. Why would parent.main.location.href be undefined once I get to the al_sum.htm page?

Quote:
quote:Originally posted by Imar
 The i in this example is an index variable used inside the loop.

This code loops while i is less than or equals to the number of quiz items. It does this by giving i a starting value of 0. Then the code between the brackets will run. On the next iteration, i is incremented by one (i++). If i is still less than or equal to quiz.length-1, the code will continue.

So, imagine the quiz had 4 questions, the code would loop for i = 0, 1, 2 and 3.

Inside the loop, you can then use i to retrieve a specific item, usually related to the value of i, as in this example:

quiz[i+1].src

In this example, the ith + 1 item is retrieved from the array with quiz questions.

Does this help?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Clay Hess
 
Old June 4th, 2004, 10:39 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

To be honest, I don't know. What is quizPage for a thing? How does the rest of the code look like?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old June 4th, 2004, 10:41 AM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is the quizpage object code...

// constructor for a quiz page object
function quizPage(src,time, timeLimit, tries, score, completed) {
  this.src = src
  this.time = (!time) ? 0 : time
  this.timeLimit = (!timeLimit) ? 0 : timeLimit
  this.tries = (!tries) ? 0 : tries
  this.score = (!score) ? 0 : score
  this.completed = (!completed) ? false : completed
}

Quote:
quote:Originally posted by Imar
 To be honest, I don't know. What is quizPage for a thing? How does the rest of the code look like?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Clay Hess
 
Old June 4th, 2004, 10:47 AM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

I got it to work by changing..

function goNext() {
  var currPage, i
  for (i=0; i<=(quiz.length-1); i++) {
    if (parent.main.location.href.indexOf(quiz[i].src) != -1) {
      parent.main.location.href = quiz[i+1].src
      break
    }

to the following..

function goNext() {
  var currPage, i
  for (i=0; i<=(quiz.length-1); i++) {
    if (parent.main.document.location.href.indexOf(quiz[i].src) != -1) {
      parent.main.document.location.href = quiz[i+1].src
      break
    }

Now I like to know the whys behind something. Why would the reference to the document make the difference?

Quote:
quote:Originally posted by Imar
 To be honest, I don't know. What is quizPage for a thing? How does the rest of the code look like?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Clay Hess
 
Old June 4th, 2004, 11:15 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I think your loop loops too many times. Take a look at this:

for (i=0; i<=(quiz.length-1); i++) {
...
      parent.main.document.location.href = quiz[i+1].src


quiz.length will return 13 (items from 0 to 12, so the code will loop for i = 0 to i = 12.

The last line tries to retrieve item i + 1, which will result in 12 + 1 for the last item in the quiz array.
Item 13 does not exist in the array, because the last item is number 12.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.