Jump to content

Primer iz javascripta?


todor27

Recommended Posts

Pocetnik sam u progrmiranju pa gledam tutorijale. Na jednom tutorijalu sam nasao sledeci primer koji kod mene ne radi, a ne mogu da provalim zasto.

 

<!DOCTYPE html>
<html>
<body>

<script>

function addPassenger (name, list){
   if(list.length == 0){
    list.push(name);
}else{
    for(var i=0; i < list.length; i++){
	    if(list[i] == undefined){
		    list[i] = name;
			return list;
		}else if(i = list.length - 1){
		    list.push(name);
			return list;
		}
	}
}
}

var passengerList = [];
passengerList = addPassenger("Greg Pollack", passengerList);
passengerList = addPassenger("Ashley Smith", passengerList);
passengerList = addPassenger("Jon Friskics", passengerList);

document.getElementById("demo").innerHTML = passengerList[0];

</script>
<p id="demo"></p>

</body>
</html>

 

U primeru treba da se u niz "passengerList" ubace neka imena pomocu funkcije "addPassenger". Medjutim prijavljuje mi gresku u Operi "TypeError: Cannot convert 'list' to object", a u fireFoxu"TypeError: list is undefined".

Da li moze neko da mi kaze u cemu je problem?

Link to comment
Share on other sites

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>


<script>

function addPassenger ( name , list) {
  if (list.length == 0) {
    list.push (name);
  } else {
    for ( var i = 0; i < list.length; i++) {
      if (list[i] == undefined) {
        list[i] = name;
        return;
      } else if (i == list.length - 1) {
        list.push(name);
        return;
      }
    }
  }
}

var passengerList = [];

addPassenger("Gregg Pollack", passengerList);
addPassenger("Ashley Smith", passengerList);
addPassenger("John Friskics", passengerList);


document.getElementById("demo").innerHTML = passengerList[1];

</script>


</body>
</html>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...
powered by