0

Anyone know why this code says "undefined"?

Code: function spyNumber() { var Spy = prompt ("Enter a random number"); } spyNumber("123"); function newFunction(notspy, spynum) { this.notspy = "126"; this.spynum = "123"; } if (this.notspy === "123") { alert ("123 is a Spy Number"); } else { alert (this.notspy + " is not a Spy Number"); }

12th May 2018, 12:54 AM
Coder Of Life
Coder Of Life - avatar
1 Antwort
+ 5
The variables you assigned (this.notspy & this.spynum) only exist within the context of your function. When you try to use them outside the function you will get that result because they are not defined. One of many possible solutions would be: function newFunction (notspy, spynum) {      this.notspy = "126";      this.spynum = "123";           if (this.notspy === "123") {          alert ("123 is a Spy Number");      }      else {          alert (this.notspy + "is not a Spy Number");      } }
12th May 2018, 1:10 AM
Mickel
Mickel - avatar