1

I have an object of Customers; and multiple instances of that objects in customers array:

Here's better picture:

customers[0].first_name = "anna";
customers[0].last_name = "dan";
customers[0].email = "[email protected]";
customers[0].credit_card = "222.324.123"

There are several of these arrays with different info: customers[1], customers[2] etc..

I need to alert everything out so it looks like this:

-----------------------------------------------------
| Customers that have signed up:                    |
|                                                   |
| anna dan    [email protected]    222.324.123       |
| contents of customer[1]                           |
| contents of customer[2]                           |
| contents of customer[3]                           |
| contents of customer[4]                           |
| and so on ...                                     |
|----------------------------------------------------

I don't need to alert the borders that you see, I just drew those to represent how the alert box should be formatted.

Here's what I tried doing which isn't working:

for (i = 0; i < customers.length; i++) {
    if (customer[i].hasSignedUp(query) == true) {
      R_fname[i] = customers[i].f_name;
      R_lname[i] = customers[i].l_name;
      R_email[i] = customers[i].email;
      R_credit[i] = customers[i].credit_card;
    }
  }

  var fin_str;

  for (i = 0; i < R_fname.length; i++) {
    fin_str += (R_fname[i] + " " + R_lname[i] + "    " + R_email[i] + "    " + R_credit[i] + " \n");
  }

  alert("Customer that have signed up " + "\n" + fin_str); 
7
  • What does "isn't working" mean? Do you get an error? Is the output not what you expected? Commented Jun 23, 2015 at 0:59
  • jsfiddle.net/cgjx1kjx Commented Jun 23, 2015 at 1:00
  • it gives me undfined Commented Jun 23, 2015 at 1:00
  • It's because you're not paying close enough attention to your array's object key names. Commented Jun 23, 2015 at 1:01
  • Check my answer out, you are setting things to be the ith index in R_ arrays but the i-1 index might not exist, this has strange effects on you second for loop. Also you don't define fin_str as an empty string and therefore the concatentated string will start with undefined Commented Jun 23, 2015 at 1:05

3 Answers 3

2

You have a few problems, I have fixed them below

var R_fname = [],
    R_lname = [],
    R_email = [],
    R_credit = [];
for (i = 0; i < customers.length; i++) {
    if (customer[i].hasSignedUp(query) == true) {
      R_fname.push(customers[i].f_name);
      R_lname.push(customers[i].l_name);
      R_email.push(customers[i].email);
      R_credit.push(customers[i].credit_card)
    }
  }

  var fin_str = "";

  for (i = 0; i < R_fname.length; i++) {
    fin_str += (R_fname[i] + " " + R_lname[i] + "    " + R_email[i] + "    " + R_credit[i] + " \n");
  }

  alert("Customer that have signed up " + "\n" + fin_str); 

Basically what I have done is define those R_ variables as empty arrays. Used .push() so the new element goes at the end and doesn't skip any indexes, and defined fin_str as an empty string so string concatenation works properly.

As others have mentioned however, logging should be done with console.log which handles lots of different formats including straight up objects / arrays very nicely.

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

2 Comments

The use of alert() appears to be part of an assignment.
Strange assignment, but whatever floats his boat :P
1

Alert isn't a good fit to print out the values of an array. Console.log would work more effectively.

1 Comment

I know, assignment forces me to use alert() ;(
0

I see you create intermediate arrays R_fname, R_lname, etc, which is unnecessary and may be confusing. You could use customers array directly:

var fin_str="";
for (i = 0; i < customers.length; i++) {
    fin_str += (customers[i].first_name + " " + customers[i].last_name + " " +customers[i].email + " " + customers[i].credit_card + "\n");
}
 alert("Customer that have signed up " + "\n" + fin_str); 

This approach is verbose and not maintenance-free (any time customers object changes, you will need to adjust this code), but you have full control of the order and formatting.

1 Comment

You completely removed part of the validation in OP's code. The hasSignedUp(query) bit to be precise. And those arrays could be used elsewhere....

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.