0

I am trying to make case 3 display array as text in the innerHTML.

For some reason its not working, the array is a string already, I converted it using a .join() method.

let userList = ["Sarah", "Bryan", "Scott", "Mirande", "Tamzin", "Tamryn", "Steven", "Peter", "Skippi", "Sheldon"]

let menu = prompt("===== Menu ===== \n" + "What would you like to do?\n\n" + "1.Add three Users\n" + "2.Remove User\n" +
  "3.Display all users \n" + "4.Sort users \n")

let choice = menu;

switch (choice) {

  case '2':
    let removeUser = prompt("Remove User");
    let removed = userList.indexOf(removeUser)
    userList.splice(removed, 1)
    break;

  case '3':
    alert("Users have been displayed");
    let arrayString = userList.join()
    document.querySelector("#display").innerHTML = arrayString
    break;

}
<div class="container">
  <h1>User List Challenge</h1>
  <div id="display"></div>
</div>
Andy
  • 61,948
  • 13
  • 68
  • 95
  • 4
    Looks like it's working fine in your example to me...? – Rory McCrossan Mar 02 '23 at 20:11
  • I get this error on my end ``Uncaught TypeError: Cannot set properties of null (setting 'innerHTML')`` – Learning Smart Mar 02 '23 at 20:12
  • 2
    Probably you have your script loaded before the rest of the HTML document. Put your script tag at the end of the `` element. – trincot Mar 02 '23 at 20:13
  • You are right, I forgot to defer it – Learning Smart Mar 02 '23 at 20:14
  • _"the array is a string already"_: no, it isn't. What are you hoping the output to look like? If you want a list you may want to add a line-break delimiter to the `join`: `.join('\n')`, and then use `innerText` rather than `innerHTML`. – Andy Mar 02 '23 at 20:15
  • Just to display, a list will look better, doesn't the .join() method join the elements in the array and put them together as a none array? I just happen to have just strings in my array – Learning Smart Mar 02 '23 at 20:20

0 Answers0