-2

I am learning if-else statements with several conditions and this easy task is somehow bothering me since the else if in the external condition is being underlined in visual studio code. I cannot seem to fix it. It says 'Declaration or statement expected'. Can you guys please take a look and help me? This is my code.

function solve(input) {
    let gender = (input.shift());
    let age = Number(input.shift());

    if (gender === 'Female') {
        if (age >= 18) {
            console.log('You are permitted on the website, lady.')
        }
    } else {
        console.log('You are not permitted on the website, lady.')
    } 
} else if (gender === 'Male') {
    if (age >= 18) {
        console.log('You are permitted on the website, dude.')
    } else {
        console.log('You are not permitted on the website, dude.')
    }
} else {
    console.log('Error')
}

solve(['Female', '13'])
  • 2
    Your `else if` should be extended off of an `if` (or `else if`) statement, but your's extends of a function block – Nick Parsons Jun 30 '19 at 12:16
  • 1
    *"in the external condition"* - What original `if` do you think `else if (gender === 'Male')` is connected to and why? – David Jun 30 '19 at 12:18
  • Indent your code properly and you will see where the issue is, you can use tools like [this](https://www.freeformatter.com/javascript-beautifier.html) if you have lots of code that you don't want to format manually. – Cray Jun 30 '19 at 12:18
  • @Cray: But that's just it... The code *is* indented properly, and that indentation is clearly demonstrating the problem. – David Jun 30 '19 at 12:19
  • @David it is connected to the external if statement regarding the gender. I don't understand what declaration I should make so that VSC stops underlining it. Now the code is not running because of this 'bug'. I just can't seem to see it. – VictoriaTodorova Jun 30 '19 at 12:26
  • 2
    @VictoriaTodorova: No it isn't. It is connected to `function solve(input) {`. Look at the indentation. Count the curly braces. – David Jun 30 '19 at 12:27

2 Answers2

1

This logic can be vastly simplified - given that you are using the same text - with only the variables of gender and age - these can be made into variables that are then inserted into the phrase that is consoled.

Just beause there are two arguments passed to the function does NOT mean that each variable needs to be within the if / else blocks

function solve(input) {
var gender, ability;

    input[0] == 'Female'
      ? gender = 'lady'
      : gender = 'dude';
      
   parseInt(input[1]) >= 18
      ? ability = 'are'
      : ability = 'are not';
      
 console.log('You ' + ability + ' permitted on the website, ' + gender + '.')
      
}

solve(['Female', '13']); // gives You are not permitted on the website, lady.
solve(['Male', '19']); // give You are permitted on the website, dude.
gavgrif
  • 15,194
  • 2
  • 25
  • 27
0

As already mentioned in the comment section, your else if statement is refering to a function block, but you have to refer to your first if statement. Realign your code/ curly brackets and your code should work as expected:

function solve(input) {
    let gender = (input.shift());
    let age = Number(input.shift());

    if (gender === 'Female') {
        if (age >= 18) {
            console.log('You are permitted on the website, lady.');
        } else {
            console.log('You are not permitted on the website, lady.');
        }
    } else if (gender === 'Male') {
        if (age >= 18) {
            console.log('You are permitted on the website, dude.');
        } else {
            console.log('You are not permitted on the website, dude.');
        }
    } else {
        console.log('Error');
    }
}
solve(['Female', '13']);

Just a recommendation, close your console log statements with a semicolon (e.g. console.log("output");). Have a look at this post, for more information about closing a statement with a semicolon.

Habebit
  • 957
  • 6
  • 23
  • Your answer was extremely helpful! Thank you so so much! This is a very simple and silly question of me but I am just in the beginning of JS and you have no idea how insightful and informative you reply is to me! It really made a difference. I figured our what my error was and I will definitely take a look at the post you linked about the semicolon. Thanks once again – VictoriaTodorova Jun 30 '19 at 13:58