How to prevent else statement from being fired every time


Starks

I'm very new to coding, sorry for wasting your time on beginner questions. I'm trying to learn JS with an ebook and my exercise atm is to write a program to save the entered name (first and last name) and the entered gender. The program should check the length of the name and report an error if the name is too long or too short. Also, the book forces me to accept only m or f when asking about gender. If everything is typed correctly, it should give something like: " Okay. Welcome to our society, fName lName! Ah, we really need other (female) males like you! " only if you typed Not m/M/f/ F For your gender, the else statement should be triggered, you should read " Sorry, we don't support gender diversity" (just kidding) but the else statement is always triggered. I'm in a different exercise Had the same problem in , so I hope I can learn from it.

let firstName = prompt('Whats your first name?');
let lastName = prompt('What is your last name?');
let gender = prompt('Please type in your gender (m or w)');

if (firstName.length >= 6 && firstName.length <= 16 && lastName.length >= 3 && lastName.length <= 12) {
  console.log(`Allright. Welcome in our society, ${firstName} ${lastName}!`);
} else {
  console.log('Sorry. One of your names is too short/long')
}


if (gender === 'm' || gender === 'M') { 
   console.log('Ah, we really want other males like you!');
} else {
  console.log('Sorry, we do not support gender-diversity here');
}
  
if (gender === 'f' || gender === 'F') {
  console.log('Ah, we really want other females like you!');
} else {
  console.log('Sorry, we do not support gender-diversity here');
}

TJ Claude

You currently have:

if (a) {
    // Output 1
} else {
    // Output 2
}
if (b) {
    // Output 3
} else {
    // Output 4
}

There is no connection between condition aand condition b, you don't want output 2 to happen just because of aan error .

Instead, use else if:

let firstName = prompt('Whats your first name?');
let lastName = prompt('What is your last name?');
let gender = prompt('Please type in your gender (m or w)');

if (firstName.length >= 6 && firstName.length <= 16 && lastName.length >= 3 && lastName.length <= 12) {
  console.log(`Allright. Welcome in our society, ${firstName} ${lastName}!`);
} else {
  console.log('Sorry. One of your names is too short/long')
}


if (gender === 'm' || gender === 'M') { 
  console.log('Ah, we really want other males like you!');
} else if (gender === 'f' || gender === 'F') {
  console.log('Ah, we really want other females like you!');
} else {
  console.log('Sorry, we do not support gender-diversity here');
}

Or you can consider switch:

let firstName = prompt('Whats your first name?');
let lastName = prompt('What is your last name?');
let gender = prompt('Please type in your gender (m or w)');

if (firstName.length >= 6 && firstName.length <= 16 && lastName.length >= 3 && lastName.length <= 12) {
  console.log(`Allright. Welcome in our society, ${firstName} ${lastName}!`);
} else {
  console.log('Sorry. One of your names is too short/long')
}

switch (gender) {
  case 'm':
  case 'M':
    console.log('Ah, we really want other males like you!');
    break;
  case 'f':
  case 'F':
    console.log('Ah, we really want other females like you!');
    break;
  default:
    console.log('Sorry, we do not support gender-diversity here');
    break;
}


(I strongly recommend against restricting binary gender selection in 2019, but I think it's a bit relevant to your question.)

Related


How to prevent else statement from being fired every time

Starks I'm very new to coding, sorry for wasting your time on beginner questions. I'm trying to learn JS with an ebook and my exercise atm is to write a program to save the entered name (first and last name) and the entered gender. The program should check the

How to prevent cURL from being fired twice in a WP function?

Morris When publishing a WordPress post, the publish_post hook is sometimes called multiple times. Therefore, the function send_webhook is also triggered multiple times. I've tried to prevent it with a global variable, but that doesn't seem to work. I think th

How to prevent cURL from being fired twice in a WP function?

Morris When publishing a WordPress post, the publish_post hook is sometimes called multiple times. Therefore, the function send_webhook is also triggered multiple times. I've tried to prevent it with a global variable, but that doesn't seem to work. I think th

How to prevent cURL from being fired twice in a WP function?

Morris When publishing a WordPress post, the publish_post hook is sometimes called multiple times. Therefore, the function send_webhook is also triggered multiple times. I've tried to prevent it with a global variable, but that doesn't seem to work. I think th

How to prevent cURL from being fired twice in a WP function?

Morris When publishing a WordPress post, the publish_post hook is sometimes called multiple times. Therefore, the function send_webhook is also triggered multiple times. I've tried to prevent it with a global variable, but that doesn't seem to work. I think th

How to prevent cURL from being fired twice in a WP function?

Morris When publishing a WordPress post, the publish_post hook is sometimes called multiple times. Therefore, the function send_webhook is also triggered multiple times. I've tried to prevent it with a global variable, but that doesn't seem to work. I think th

How to prevent cURL from being fired twice in a WP function?

Morris When publishing a WordPress post, the publish_post hook is sometimes called multiple times. Therefore, the function send_webhook is also triggered multiple times. I've tried to prevent it with a global variable, but that doesn't seem to work. I think th

How to prevent cURL from being fired twice in a WP function?

Morris When publishing a WordPress post, the publish_post hook is sometimes called multiple times. Therefore, the function send_webhook is also triggered multiple times. I've tried to prevent it with a global variable, but that doesn't seem to work. I think th

How to prevent cURL from being fired twice in a WP function?

Morris When publishing a WordPress post, the publish_post hook is sometimes called multiple times. Therefore, the function send_webhook is also triggered multiple times. I've tried to prevent it with a global variable, but that doesn't seem to work. I think th

How to prevent cURL from being fired twice in a WP function?

Morris When publishing a WordPress post, the publish_post hook is sometimes called multiple times. Therefore, the function send_webhook is also triggered multiple times. I've tried to prevent it with a global variable, but that doesn't seem to work. I think th