0

Let's say you have an object

var employee = [];

How would you prompt (with the prompt function) the user to input information so that it looks like this:

var employee = [
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName": "Jones"}
];

Of course with an unspecified number of arrays. So if your user only inputs information for one employee and then inputs nothing or hits cancel the array will only have one array (for John Doe).

The main thing though is that the user inputs information until they decide that they are done. So an unspecified number of arrays.

I need to do this with a looping prompt even though I understand that prompts suck. A lot.

So here's what I'm trying to do:

  1. Prompt user for an employee. Example input: John Doe
  2. Take string, use split function. After which you would have an array named John Doe
  3. Loop the above until user inputs "" or clicks cancel.
  4. Store all the employees into an object that can be printed to the console.

This is what my code looks like.

var person = {firstname: "", lastname: ""}
var employee = []; //used to store each person

var input = "x"; 

for(var i = 0; input != "" && input != null; i++){
    var input = prompt("Input first and last name"); 

    var results = input.split(); //create an array from the input

    person.firstname = results[0];
    person.lastname = results[1];

    employee[i] = person;//store the person array into the customer array.
}  

console.log(employee[1]["firstname"]); //test output
console.log(employee[0]["lastname"]); //test output

when I test my output however I get undefined. I am trying to access a specific person's first name but an unable to.

3 Answers 3

2

You can do it like this

var flag = true, arr = [];
while (flag) {
    arr.push({
        firstname: prompt("What's your first name?") || "John",
        lastname: prompt("What's your last name?") || "Doe"
    });
    flag = confirm("Do you want to continue?");
}

Or else, you can also do

var arr = (prompt("What's your name with first and last name") || "John Doe").split(" ");
arr.push({
    firstname: arr[0],
    lastname: arr[1]
});
Sign up to request clarification or add additional context in comments.

Comments

1

You're calling split() but not providing any parameters. If it's a name, I assume first and last are split by spaces. It should be something like:

var results = input.split(" ");

Comments

0

Their suggestions are all correct.

And the reason why your code is not working is because you only have single instance of person object, and all of your employee array values reference to this person object. Once you input "", your person object property got an undefined value, so your values in employee array will also be undefined.

you can modify person object to be multiple instances to fixes this error.

function person() {
            return {firstname: "", lastname: ""};
        }
        ;
        var employee = []; //used to store each person

        var input = "x"; 

        for(var i = 0; input != "" && input != null; i++){
            var input = prompt("Input first and last name"); 

            var results = input.split(); //create an array from the input
            per = person();

            per.firstname = results[0];
            per.lastname = results[1];

            employee[i] = per;//store the person array into the customer array.
        }

        console.log('hi');
        console.log(employee[1].firstname);

fiddle here

2 Comments

When I tried your code it resulted in print out the entire name instead of just the first name. It would print John Doe instead of just John even though the code was alert(employee[1].firstname);
Yes, you are correct! This is because you missing add the delimeter in the split function, it should contains a space there: var results = input.split(' '); fiddle has been updated now, you can try it again [link]jsfiddle.net/mLrdjcdp

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.