if statement is not returning expected result Javascript

I wrote a small script after learning a bit about the object constructor and I wanted to take it a step further with this code, but the if statement isn't behaving as expected... All I want to know is why? I understand this kind of script isn't a normal thing in Javascript... but I believe that the === is evaluating to true, thus printing dave in the alert box, no matter what I enter, and not following through the if statement conditions.

var dave, jason, nick, get;

get = prompt('Input your Name', 'Name Here')


var Person = function (name, birthYear, job) {
    this.name = name;
    this.birthYear = birthYear;
    this.job = job;
};

dave = new Person('Dave', 1976, 'Showman');
jason = new Person('Jason', 1987, 'Novice');
nick = new Person('Nick', 1993, 'Cable-tech');


function printObj(object) {
    var output = '';
    for (var property in object) {
        output += property + ': ' + object[property] + '\n';
    }
    alert(output);
}

if (get.toLowerCase === 'dave'.toLowerCase) {
    printObj(dave);
} else if (get.toLowerCase === 'jason'.toLowerCase) {
    printObj(jason);
} else if (get.toLowerCase === 'nick'.toLowerCase) {
    printObj(nick);
} else {
    alert('Not a defined object')
}

Something else obvious I might be doing wrong is the comparison... but that's how it was explained to me in javascript on freenode, because at first that was my issue, which is still kind of it I suppose. I think I'm simply doing something obviously messy, appreciate any insight I receive, the MDN and W3 only explain so much.

2 answers

  • answered 2018-07-11 03:17 Spidy

    get.toLowerCase() is a method, and should be invoked with the open and closed parenthesis. The way it is currently written is checking whether the method is equal to the same method on a different string instance.

    if (get.toLowerCase() === 'dave') {
        printObj(dave);
    } else if (get.toLowerCase() === 'jason') {
        printObj(jason);
    } else if (get.toLowerCase() === 'nick') {
        printObj(nick);
    } else {
        alert('Not a defined object')
    }
    

    Since you are comparing to a lowercase string, you don't need to call toLowerCase() on 'dave', 'jason', or 'nick'. I think you may have meant to compare to the value stored in the variables dave, jason, and nick.

    if (get.toLowerCase() === dave.name.toLowerCase()) {
        printObj(dave);
    } else if (get.toLowerCase() === jason.name.toLowerCase()) {
        printObj(jason);
    } else if (get.toLowerCase() === nick.name.toLowerCase()) {
        printObj(nick);
    } else {
        alert('Not a defined object')
    }
    

    Last, you should not name a variable get. get is reserved in ES6 so as browser start to natively support it, your code will do some very odd things. I suggest something like userInput.

    var dave, jason, nick, userInput;
    
    userInput = prompt('Input your Name', 'Name Here')
    
    //...other code...
    
    if (userInput.toLowerCase() === dave.name.toLowerCase()) {
        printObj(dave);
    } else if (userInput.toLowerCase() === jason.name.toLowerCase()) {
        printObj(jason);
    } else if (userInput.toLowerCase() === nick.name.toLowerCase()) {
        printObj(nick);
    } else {
        alert('Not a defined object')
    }
    

  • answered 2018-07-11 04:44 user10109713

    You are using the .toLowerCase() method the wrong way. Also, you are using the get

    Your Code:

    var dave, jason, nick, get;
    
    get = prompt('Input your Name', 'Name Here')
    
    // other code
    
    if (get.toLowerCase === 'dave'.toLowerCase) {
        printObj(dave);
    } else if (get.toLowerCase === 'jason'.toLowerCase) {
        printObj(jason);
    } else if (get.toLowerCase === 'nick'.toLowerCase) {
        printObj(nick);
    } else {
        alert('Not a defined object')
    }
    

    As you can see on your code your .toLowerCase method misses the (). And please do not use the get as a variable name because get is a keyword.

    Try this:

    var dave, jason, nick, inputValue;
    
    inputValue = prompt('Input your Name', 'Name Here')
    
    // other code
    
    if (inputValue.toLowerCase() === 'dave'.toLowerCase()) {
        printObj(dave);
    } else if (inputValue.toLowerCase() === 'jason'.toLowerCase()) {
        printObj(jason);
    } else if (inputValue.toLowerCase() === 'nick'.toLowerCase()) {
        printObj(nick);
    } else {
        alert('Not a defined object')
    }
    

    Also, if you are comparing an own defined comparison there's no need to add the .toLowerCase(). You actually want to compare the input value (which is an unknown format ei. upper case or lower case) to a lower case string value that you want to compared of.

    Use this:

    if (get.toLowerCase === 'dave')
    

    instead of

    if (get.toLowerCase === 'dave'.toLowerCase())