user146881
user146881

Reputation: 1

Javascript console says: Invalid left-side assignment?

So, this is to make a secure login. i run this through chrome's javascript console and i get 'Uncaught ReferenceError: Invalid left-hand side in assignment '?! please help!

function logIn() {
String.prototype.hashCode = function() {
        for(var ret = 0, i = 0, len = this.length; i < len; i++) {
        ret = (31 * ret + this.charCodeAt(i)) << 0;
    }
        return ret;
    };

var user = document.getElementById("username"),
    pass = document.getElementById("password");
if ((user = "Fedora") && (pass.hashCode() = -976887139)) {
    window.alert("Captain Fedora: SuperUser");

}
}   

Upvotes: 0

Views: 1087

Answers (3)

caoglish
caoglish

Reputation: 1361

if ((user = "Fedora") && (pass.hashCode() = -976887139))

change to

if ((user == "Fedora") && (pass.hashCode() == -976887139))

also 'reuturn ret' may return undefined because in wrong scope. var ret = 0 only work in 'for' scope

Upvotes: 0

Bergi
Bergi

Reputation: 664920

Yes, pass.hashCode() = -976887139 is an assignment. And since the function call doesn't return a reference, it's invalid to assign to it. You probably wanted the comparison

pass.hashCode() == -976887139

Also, you will want to get the values of those DOM elements (I can't believe the password input has a hashCode method at all), and you also want to compare the content of the user variable with that string instead of assigning them.

var user = document.getElementById("username").value,
    pass = document.getElementById("password").value;
if (user == "Fedora" && pass.hashCode() == -976887139) {
    window.alert("Captain Fedora: SuperUser");
}

Upvotes: 0

dave
dave

Reputation: 64657

if ((user = "Fedora") && (pass.hashCode() = -976887139))

should be

if ((user == "Fedora") && (pass.hashCode() == -976887139))

so you do comparison, and not assignment

Upvotes: 3

Related Questions