6. we made a friend, stuck on syntax error, please help


#1



6.We made a friend


hi guys i've made a silly mistake in my code but can't see it, i know i'm gonna kick myself when i spot it.
error is: SyntaxError: Unexpected token )
it's either in the add function or the main switch loop at the bottom i think
any help greatly appreciated, thanks in advance


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]);
	}
}

/*Create a search function
then call it passing "Jones"*/
function search(lastName) {
    var match=false;
    for (var i = 0; contacts[i]; i++) {
        if (lastName===contacts[i].lastName) {
            printPerson(contacts[i]);
            match=true;
        };
    }
    if (match===false) {
        console.log("sorry no dice");
    }
}
//add function adds record to contacts
function add(firstName, lastName, email, phoneNumber) {
    contacts[contacts.length] = {
        firstName: firstName,
        lastName: lastName,
        email: email,
        phoneNumber: phoneNumber
    };
    
}

//main function below loops while you add or search
var again=true;
for (again) {
    var entry = prompt("add/search/list");
//if add selected askes for details runs add function
    switch (entry) {
        case 'add':
            var first = prompt("firstname");
            var last = prompt("lastname");
            var mail = prompt("email");
            var phone = prompt("phone");
            add(first, last, mail, phone);
            console.log(first +" "+ last +" added");
            break;
    
//if search selected askes for name and runs search function
        case 'search':
            search(prompt("enter name to search"));
            break;
    
//if anything else entered it exits loop
        default:
            again = false;
            break;
    }
}


#2

this is far more code then the exercise requires? That can be very risky.

Either way, there is a huge problem in this line:

for (again)

a for loop has 3 parts, initialize, condition, and change:

for (i = 0; i < 10; i++){}

i think in your case you should use a while loop?


#4

ah yes of course knew would be silly mistakes that has now changed the error
am now getting "SyntaxError: Unexpected string"
tried removing different sections of switch statement but not got anywhere

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]);
	}
}

/*Create a search function
then call it passing "Jones"*/
function search(lastName) {
    var match=false;
    for (var i = 0; contacts[i]; i++) {
        if (lastName===contacts[i].lastName) {
            printPerson(contacts[i]);
            match=true;
        };
    }
    if (match===false) {
        console.log("sorry no dice");
    }
}
//add function adds record to contacts
function add(firstName, lastName, email, phoneNumber) {
    contacts[contacts.length] = {
        firstName: firstName,
        lastName: lastName,
        email: email,
        phoneNumber: phoneNumber
    };
    
}

//main function below loops while you add or search
var again=true;
while (again) {
    var entry = prompt("add/search/list/exit");
//if add selected askes for details runs add function
    switch (entry) {
        case 'add':
            var first = prompt("firstname");
            var last = prompt("lastname");
            var mail = prompt("email");
            var phone = prompt("phone");
            add(first, last, mail, phone);
            console.log(first +" "+ last +" added");
            break;
    
//if search selected askes for name and runs search function
        case 'search':
            search(prompt("enter name to search"));
            break;
//case list 
        case 'list':
            list();
            break;
//case exit  
        case 'exit':
            again = false;
            break;
//if anything else entered it exits loop
        default:
            console.log"sorry didn't catch that, type exit to quit");
            break;
    }
}

#5

this line:

console.log"sorry didn't catch that, type exit to quit");

your console.log is missing its opening bracket ((), which is rather risky


#6

ahh thank you wasn't looking for the mistake in the simple bit had to restart pc after accidentally commented out too much while trying to narrow it down and crashed laptop. lol. thanks again was at wits end trying to find that. lol
yeah is more complicated than should be but wanted to test myself a bit and make it a bit more functional.
one bit that does confuse me is in the array each object has a name ie bob, susan however when adding a new object couldn't think of a way to add the name as normally use var (so the variable i'm trying to enter would be treated as a new variable instead of using the string it refers to)
is there a way i could do this was wondering about new Object function but as the is no defined constructor wasn't sure how to call it without parent object