4/6 Result appears twice


#1

var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-7777",
email: "bob.jones@example.com"
};

var mary = {
firstName: "Mary",
lastName: "Johnson",
phoneNumber: "(650) 888-8888",
email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}

var list = function (){
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++)
{
printPerson(contacts[i]);
}
}

list();

This code prints the result twice.

Bob Jones
Mary Johnson
Bob Jones
Mary Johnson

How can I remove the redundancy. Help Please!


#2

I have the exact same thing.

var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-7777",
email: "bob.jones@example.com"
};

var mary = {
firstName: "Mary",
lastName: "Johnson",
phoneNumber: "(650) 888-8888",
email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}

function list(){
var contactsLength = contacts.length;
for(var i = 0; i < contactsLength; i++){
printPerson(contacts[i]);
}
};

list();


#3

So do I... And says that everything is right and I can go on lessons...


#4

Me too. I don't know how to fix it I tried doing this

for(var i = 0; i < contactsLength - 1; i++){

but that just gave me.

 Bob Jones
 Bob Jones

#5

I have the same code, and get the same redundancy. However, I don't really think this is a mistake.

At the bottom of the instructions for 3/6, it says: "Don't worry if your output appears twice - we're just double checking your code!" -- but (at least for me) the output for that lesson does NOT actually appear twice. I think it's simply the case that this line should have been appended to lesson 4/6 rather than 3/6.

I could be wrong of course, but it's the best explanation I can think of.


#6

This is my code. It worked, but I would like to remove the 'undefined' in between my results.

var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-7777",
email: "bob.jones@example.com"
};

var mary = {
firstName: "Mary",
lastName: "Johnson",
phoneNumber: "(650) 888-8888",
email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}

var list = function (){
var contactsLength = contacts.length;
for (i = 0; i < contactsLength; i++){
console.log( printPerson(contacts[i]));
}
};

list();

The results came out like this:

Bob Jones
undefined
Mary Johnson
undefined
Bob Jones
undefined
Mary Johnson
undefined


#7

I honestly don't have a clue what I'm doing but this worked for me without repeating anything.

var list = function(){
var contactsLength = contacts;
}
for (i = 0; i < contacts.length; i++){
printPerson(contacts[i]);
}
list();

I'm sure there's lots wrong, probably the contactsLength variable in particular but maybe it'll help someone here.


#8

This will have 2 console.log

function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}

var list = function (){
var contactsLength = contacts.length;
for (i = 0; i < contactsLength; i++){
console.log( printPerson(contacts[i]));
}


#9

This will work without printing out undefined.

function list() {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
printPerson(contacts[i]);
}

}


#10

I think that your mistake it's your call to "list();", I've passed and I used this code:

var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-7777",
email: "bob.jones@example.com"
};

var mary = {
firstName: "Mary",
lastName: "Johnson",
phoneNumber: "(650) 888-8888",
email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}

function list() {
var contactsLength = contacts.length;
}

for(i = 0; i < contacts.length; i++) {
printPerson(contacts[i])
}


#11

That code doesn't use the list() function in the for loop, though, which the instructions say to do. If you add a third person, it prints three times; a fourth, four times, etc. It's as if it's in a nested loop, where each call to printPerson() in the list() function prints the whole list, despite being given an index.

var contacts = [bob, mary, ann];

function printPerson (person) {
    console.log(person.firstName + " " + person.lastName);
}

var list = function() {
    var num = contacts.length;
    for (var i = 0; i < num; i++) {
        console.log(i);
        printPerson(contacts[i]);
    }
};
list();

yields
0
Bob Jones
1
Mary Johnson
2
Ann Smith

and repeats that two more times. All I can think is that this is an error in the interpreter or compiler, whatever CodeAcademy is using to run the scripts.


#12

Oh I see. So funny how you don't catch those things.I definitely didn't see it in there twice.


#13

There is no error at all people. Its because of this code you did at chapter before this

function printPerson(person) {
console.log(person.firstName + " " + person.lastName);

It is still printing the previous code since they still exist within the code.


#14

function list() {
var contactsLength = contacts.length;
}
for(i = 0; i < contacts.length; i++) {
printPerson(contacts[i])
}
This code works, no redundancy.


#15

instead of using

use
for(var i = 0; i < contactsLength ; i++)