0

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");

}
}   
  • 1
    A cleaver trick to avoid making this make is called Yoda conditions, train yourself to write if statements like this: `if("Fedora" == user) {`, that way if you forget the second equals you will get an exception as technically assignments in if statements are valid JavaScript. – Jason Sperske Apr 17 '13 at 23:47
  • Run your code through a validator like http://jshint.com/ to help in your basic debugging. –  Apr 18 '13 at 00:21

3 Answers3

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

should be

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

so you do comparison, and not assignment

dave
  • 62,300
  • 5
  • 72
  • 93
0

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");
}
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0
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

caoglish
  • 1,343
  • 3
  • 19
  • 29