简体   繁体   中英

Using For Loops and Arrays on Javascript to display phone number when name is entered

I want to set up a prompt so that when a user enters a username, it will display the phone number for that username.

So far when I run the code below, all it returns is the first username.

What am I doing wrong?

 var question1 = prompt("Enter the username of a friend") var i = 5; var friends = ["Luke M. ", " Chloe M. ", "Cindi G. ", "Pete D. ", "Lola D. "]; var number1 = ["888-888-8888 ", "000-000-0000 ", "111-111-1111 ", "222-222-2222 ", "333-333-3333 "]; for (var i = 0; i <= friends; i++) { } document.write(friends[i] + number1[i]); 

A few things:

  • you must put your document.write inside the for loop
  • use friends.length instead of i since i is the index number not the length of the array
  • use the .length to get the length of the array friends
  • Your array has white spaces in the string elements you may want to trim to check for spacing issues withm .trim()

EDIT - Also when using a for loop and starting at 0 use 'less than' to not get an off by one error.

 var question1 = prompt("Enter the username of a friend") var friends = ["Luke M. ", " Chloe M. ", "Cindi G. ", "Pete D. ", "Lola D. "]; var number1 = ["888-888-8888 ", "000-000-0000 ", "111-111-1111 ", "222-222-2222 ", "333-333-3333 "]; for (var i = 0; i < friends.length; i++) { if(question1 == friends[i].trim() || question1 == friends[i]){ document.write( number1[i] ); } } 

edited to only show number not name

I would recommend you to use array's indexOf function:

var friendIndex = friends.indexOf(question1);

if (friendIndex > -1) {
  document.write( friends[friendIndex ] + number1[friendIndex ] );
}

You could make use of the forEach() method:

 var question1 = prompt("Enter the username of a friend") var friends = ["Luke M. ", " Chloe M. ", "Cindi G. ", "Pete D. ", "Lola D. "]; var number1 = ["888-888-8888 ", "000-000-0000 ", "111-111-1111 ", "222-222-2222 ", "333-333-3333 "]; friends.forEach(function(friend, i) { if(friend.trim() === question1) { document.write( number1[i] ); } }); 

This is because you don't have any code executing in the for loop.

If you do this:

  var question1 = prompt("Enter the username of a friend") var i = 5; var friends = ["Luke M. ", " Chloe M. ", "Cindi G. ", "Pete D. ", "Lola D. "]; var number1 = ["888-888-8888 ", "000-000-0000 ", "111-111-1111 ", "222-222-2222 ", "333-333-3333 "]; for (var i = 0; i < friends.length; i++) { if(question1 === friends[i].trim()){ document.write( friends[i] + ':' + number1[i] ); } } 

Then, I believe that you will have what you are looking for.

(Of course, you will need to modify the above code to have it do what you need it to do aside from dumping the name.)

NOTE: The conditional in the for loop i < friends.length was modified from your original code as the array index has the range 0..friends.length - 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM