LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 June 17 2016

anonymous
Member

JavaScript program, need help

Can someone please check what's wrong with my code?

/**
 * Created by user on 6/16/2016.
 */
function Person(fname,lname,phone,email) {
    this.fname = fname;
    this.lname = lname;
    this.phone = phone;
    this.email = email;
}
var wafaa = new Person("Wafaa","Temsah","xx","[email protected]");
var walid = new Person("Walid","Khaled","xx","");
var fatima = new Person("Fatima","Jeradi","xx","");
var aliz = new Person("Ali","Zaraet","7xx"," ");
var hadi = new Person("Hadi","Hammoud","7xx","[email protected]");
var mesh = new Person("Mohammad","Abed","xx","[email protected]");
var contacts= [];
contacts[0]=wafaa;
contacts[1] = walid;
contacts[2]= fatima;
contacts[3]= aliz;
contacts[4]=hadi;
contacts[5]=mesh;
var view = function(contacts){
    return contacts;
};
var i = contacts.length;
var add = function() {
    var first = prompt("What's his first name?");
    var last = prompt("What's his last name?");
    var number = prompt("Put his number");
    var email = prompt("What's his email?");
    contacts[i]=new Person(first, last, number, email);
};
var answer = prompt("Welcome to my contacts!! Do you want to add a contact search amongst the existing or view the existng contacts?","type \"add\" or \"search\" or \"view\"");
if (answer === "add") {
    add();
} else if (answer === "view") {
    view(contacts);
}
 else if(answer ==="search"){
    var search = prompt("type in");
    for (var prop in contacts) {
        if (search== (prop.fname || prop.lname || (prop.fname + prop.lname) || (prop.phone) || prop.email)){
            console.log(prop);
        }


        else{
            console.log("rhis contact doesn't exist");
        }
    }

}
else{
    console.log("I didn't get you!");}

Last edited by anonymous (June 17 2016)

Offline

#2 June 17 2016

rtp
Member

Re: JavaScript program, need help

I found one problem with the search, am not sure if you are having any other problems, if so, please elaborate.  With Chrome you can use the keyword "debugger" and the browser will stop at it... Like below

    var search = prompt("type in");
	debugger;

You shouldnt use for..in
For....in is used to iterate over property and not array
https://developer.mozilla.org/en-US/doc … s/for...in

Use a normal "for" instead

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

Offline

#3 June 17 2016

rolf
Member

Re: JavaScript program, need help

Why don't you start telling us what's wrong?

Offline

#4 June 17 2016

Elied
Member

Re: JavaScript program, need help

Please review the forum rules on how to ask for help and edit your post accordingly. I doubt asking in this manner will get you any help

Offline

#5 June 18 2016

samer
Administrator

Re: JavaScript program, need help

Offline

Board footer