0

I want the user to insert some data about himself,to push user's input to my array for the further usage.Can I push the data to the array and make it multidimensional? How to convert an empty array to the multidimensional one ? Here is my http://jsfiddle.net/arminemash/r1pfwwe4/114/. Thanks in advance

  <script>
   var dataBaze=[];

   function myFunction(){ 
      alert('hello');
      $("button").click( inputData());  
    };

  function inputData(){ 
    do{

      var input=prompt("Enter user's name,surname,telephone number,Email keeping the order and separating words by a comma"," ");   
     dataBaze.push(input);  
    var ok=confirm("continue?");
  } 
  while(ok==true);

}
</script>
4
  • Split the users input by ',' then push that. However prompts and spliting on a character is terrible ux and brittle. Consider implementing a form instead. Commented Jul 3, 2016 at 13:21
  • ste2425,Thank you for your comment, I am new in JS and don't exactly catch the meaning of your suggestion.How can I do that? Commented Jul 3, 2016 at 13:25
  • 2
    $("button").click(inputData()); needs to be $("button").click(inputData);. Commented Jul 3, 2016 at 13:26
  • I'm on phone so cant write a full answer. However search for string.split and how to implement a form. There will be plenty of info on both. Commented Jul 3, 2016 at 13:28

3 Answers 3

2

First, since you get the data from a prompt separated by comma, you need to split the string to make it an array. To do so :

var infos = prompt("Enter user's name,surname,telephone number,Email keeping the order and separating words by a comma"," ") var infosArray = infos.split(','); dataBaze.push(infosArray);

The split method lets you take a string and divide it into chunks with the delimiter you pass into the function. So ".split(',')" finds every instance of a comma and takes what is before it and push it into an array. When the full string has been parsed, it returns the full array.

From there, each cell of the array will contain every information in its own sub-cell (dataBaze[0] may contain something like

['MyName', 'MySurname', '555-555-5555', 'myemail@email]

So let's say you want the name you could use "dataBaze[0][0]", and so forth.

However, there are a couple of ways to make things a little bit easier to read and to maintain, such as inserting an object into the array, something like so:

var user = {name:'', surname:'', telephone:'', email:''};
user.name = infosArray[0];
user.surname = infosArray[1];
user.telephone = infosArray[2];
user.email = infosArray[3];
dataBaze.push(user);

Then you can access infos like so :

document.write("My name is " + dataBaze[0].name + ", and my surname is " + dataBaze[0].surname + ". You can call me at " + dataBaze[0].telephone + " or contact me by email at " + dataBaze[0].email +". Thanks!");

What we've done here is create an object ({}) which is basically a keyed array (it is more but let's not get too deep for no reason). So when you come back to your code later, you don't have to guess every time which cell is what.

Edit: Just thought I'd add some explanation to the what and whys.

Sign up to request clarification or add additional context in comments.

Comments

2

As Xufox said, use the function name to call it like this :

$("button").click(inputData);

And while pushing your data into the array, push it like this :

var input = prompt("Enter user's name,surname,telephone number,Email keeping the order and separating words by a comma", " ").split(",");
dataBaze.push({
  "name":input[0],
  "surname":input[1],
  "telephone":input[2],
  "email":input[3]
});

Therefore, you will get a multidimensional array as you wish. Here is the jsFiddle

And as ste2425 said, you can use form instead,

var dataBaze = [];

$("#submit").click(function(){
    dataBaze.push({
    	"name": $("#name").val(),
      "surname":$("#surname").val(),
      "telephone":$("#phone").val(),
      "email":$("#email").val(),
    });
    console.log(dataBaze)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  Enter name: <input type="text" id="name"><br/>
  Enter Surname: <input type="text" id="surname"><br/>
  Enter Telephone: <input type="text" id="phone"><br/>
  Enter Email: <input type="text" id="email"><br/>
  <button id="submit">submit</button><br/>
</form>

Hope it works :)

3 Comments

Although this is nice approach, it isn't a multidimensional array. It's a simple array of objects, isn't it? Just to keep the terminology straight, but +1 from me.
Thank you for your help but it must be prompt window.
@Armine No problem man. Just thought form would be better than prompt :)
0

I suppose you want to create something like this:

function inputData(){   
    var counter = 0;
    do{ 
        var input=prompt("Enter user's name,surname,telephone number,Email keeping the order and separating words by a comma"," "); 
    elements = input.split(",");
    dataBaze[counter] = new Array(5);
    for (var j = 0; j < 5; j++) {
      dataBaze[counter][j]= elements[j];
      }         
    counter++;
        var ok=confirm("continue?");
    } 
    while(ok==true);
  //alert is for checking;
  alert(dataBaze);
}

You can simplify it in this way:

var elements = infos.split(','); 
dataBaze.push(infosArray);

1 Comment

I want to create somthing like dataBaze=[['John','Smith',1254,'[email protected]'],['Sara','Frank',4789,'[email protected]'],['David','Malan',7868,'[email protected]']] and to fill this data into a table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.