AZNPNOY2000
AZNPNOY2000

Reputation: 31

New to Javascript and Programming. Error with my code?

Hi I'm new to JavaScript and programming in general. I've been using Codecademy to help me learn. However, I've recently encountered a problem with a set of code I've created within Codecademy and I can't seem to find what's the problem. Here's the code:

var mInQuestion = prompt('Do you play a musical instrument?').toLowerCase;
if (mInQuestion == yes) {
var musInstrument = prompt('Which instrument do you play?').toLowerCase;
switch (musInstrument) {
    case 'piano':
        console.log('Oh great! Piano is wonderful!');
        break;
    case 'guitar':
        console.log('Wow I love guitar as well!');
        experience = prompt('Did you play when you were little?').toLowerCase;
        interest = prompt('And do you like playing it?')toLowerCase;
        if (experience && interest == yes||y) {
            console.log("That's great! You must be a guitar master!");
        }
        else {
            console.log("Keep learning! It will be great. Or you could do piano?");
        }
        break;
    case 'flute':
        console.log('Oh that\'s cool! You know I used to play flute as well!');
        break;
    default:
        console.log('Oh I\'m not sure I\'ve heard of that instrument...sorry');
        break;
}
else if (mInQuestion == no) {
console.log('Oh you don\'t? You should pick one like the piano or the guitar!');
}
else {
console.log('Your answer doesn\'t make sense...');
}

Upvotes: 0

Views: 85

Answers (6)

Eric Hartmann
Eric Hartmann

Reputation: 776

You've got a few problems with your guitar. You need to declare the two variables experience and interest, plus you were missing a . before toLowerCase. you were also missing a closing bracket } on the end of your if statement. Your if statements also need to be formatted with quotes if you are testing for a string, and each condition needs to be written out fully.

 var mInQuestion = prompt('Do you play a musical instrument?').toLowerCase;
if (mInQuestion == yes) {
    var musInstrument = prompt('Which instrument do you play?').toLowerCase;
    switch (musInstrument) {
        case 'piano':
            console.log('Oh great! Piano is wonderful!');
            break;
        case 'guitar':
            console.log('Wow I love guitar as well!');
            var experience = prompt('Did you play when you were little?').toLowerCase; //need to declare these
            var interest = prompt('And do you like playing it?').toLowerCase;
            if ((experience ==='yes' ||experience==='y') && (interest === 'yes'|| interest ==='y')) {
                console.log("That's great! You must be a guitar master!");
            }
            else {
                console.log("Keep learning! It will be great. Or you could do piano?");
            }
            break;
        case 'flute':
            console.log('Oh that\'s cool! You know I used to play flute as well!');
            break;
        default:
            console.log('Oh I\'m not sure I\'ve heard of that instrument...sorry');
            break;
    }} //need bracket here
else if (mInQuestion == no) {
    console.log('Oh you don\'t? You should pick one like the piano or the guitar!');
}
else {
    console.log('Your answer doesn\'t make sense...');
}

Upvotes: 1

mwilson
mwilson

Reputation: 12960

LOTS of syntax errors. You have variables that are comparing the input in the prompt like mInQuestion == yes which is incorrect (unless you had a variable called yes. This needs to be changed to mInQuestion == 'yes'. THere are many other issues similar to this throughout your code. In addition, you're also missing a closing curly brace } on your first if statement. You also forgot to invoke some of your functions such as toLowerCase().

var mInQuestion = prompt('Do you play a musical instrument?').toLowerCase(); // <--forgot to invoke
if (mInQuestion == 'yes') { // <-- changed to 'yes'
    var musInstrument = prompt('Which instrument do you play?').toLowerCase(); // <--forgot to invoke
    switch (musInstrument) {
        case 'piano':
            console.log('Oh great! Piano is wonderful!');
            break;
        case 'guitar':
            console.log('Wow I love guitar as well!');
            experience = prompt('Did you play when you were little?').toLowerCase(); // <--forgot to invoke
            interest = prompt('And do you like playing it?').toLowerCase(); // <--forgot to invoke and forgot period
            if (experience && interest == 'yes' || 'y') { // <-- changed to 'yes' and 'y'
                console.log("That's great! You must be a guitar master!");
            }
            else {
                console.log("Keep learning! It will be great. Or you could do piano?");
            }
            break;
        case 'flute':
            console.log('Oh that\'s cool! You know I used to play flute as well!');
            break;
        default:
            console.log('Oh I\'m not sure I\'ve heard of that instrument...sorry');
            break;
    }
} // <--Added closing curly brace
else if (mInQuestion == 'no') { // <-- changed to 'no'
    console.log('Oh you don\'t? You should pick one like the piano or the guitar!');
}
else {
    console.log('Your answer doesn\'t make sense...');
}

Upvotes: 0

Brennan
Brennan

Reputation: 5742

Look at this line here:

if (mInQuestion == yes) {

You are trying to check if mInQuestion is the string "yes", but you are actually checking to see if it is equal to a variable yes that doesn't exist. Put quotes around the yes and no.

Also note this line:

if (experience && interest == yes||y) {

You can not check multiple conditions like this, you must be explicit with each check:

if ((experience == 'yes' || experience == 'y') && (interest == 'yes' || interest == 'y'))

Also, everywhere you are trying to call toLowerCase is incorrect. You are trying to call a function, but are leaving off the parentheses. This means that your variables are actually functions, not strings like you intended. Put parentheses after each call to toLowerCase

Upvotes: 3

Andrew Yee
Andrew Yee

Reputation: 11

There are several bugs/errors in your js.

Here is one of them:

    if (experience && interest == yes||y) {
        console.log("That's great! You must be a guitar master!");
    }

should be

    if ( (experience == yes|| y ) && ( interest == yes||y) ) {
        console.log("That's great! You must be a guitar master!");
    }

because the way it was written previously, (experience && interest == yes||y), experience just needs to have a value (it can be anything but 0 or null ) and it will pass as True.

Upvotes: -3

T.J. Crowder
T.J. Crowder

Reputation: 1074989

Three problems:

  1. toLowerCase in the above should be toLowerCase() (everywhere). The former is a reference to the function. The latter calls the function and returns the lower-case equivalent string.

  2. Unless you have the variable yes declared somewhere, you probably want that string in quotes, e.g. "yes". Similarly for no and y (and probably n, somewhere).

  3. The one causing the Unexpected identifier error is here:

    if (experience && interest == yes||y) {
    

    To compare a variable with two values, you do them individually:

    if (experience && (interest == yes || interest == y)) {
    

Upvotes: 0

jrath
jrath

Reputation: 990

syntax error missed . here interest = prompt('And do you like playing it?')toLowerCase;

 break;
case 'guitar':
    console.log('Wow I love guitar as well!');
    experience = prompt('Did you play when you were little?').toLowerCase;
    interest = prompt('And do you like playing it?').toLowerCase;
    if (experience && interest == yes||y) {
        console.log("That's great! You must be a guitar master!");
    }

Upvotes: 0

Related Questions