0

Trying to create student register which should allow users to input their names. I want an error to prompted when the same name is entered unfortunately as I'm a beginner I'm finding it hard to achieve this. Is it possible to target all the names in the array?

function idCheck() {
  let names = ['Mark', 'John', 'Tom', 'Dave', 'Jim'];

  alert('Welcome to Club 332');

  let name = prompt('Please Enter Your Name');
  
  if (names[0] === name) {
    console.log('Unfortunately, the following name is taken, Try Again');
  } else if (name !== names) {
    names[names.length] = name;
  }

  console.log(names);

  let age = prompt('Enter Your age');
   
  if (age < 18) {
    console.log(alert('Access Denied Over 18s Only'));
    console.log('Access Denied Over 18s Only');
  } else if (age >= 18) {
    console.log(alert('Access Granted'));
    console.log('Access Granted');
  }
};

idCheck();
Marian13
  • 7,740
  • 2
  • 47
  • 51
M_Menss
  • 3
  • 1

1 Answers1

0

Just use names.includes(name) which returns true if the name is already present.

genius42
  • 243
  • 1
  • 6