I have an issue with my JavaScript code that is so stupid that I doubt anyone can help. But here it is: There is a variable called v2
and apparently if I do v2 == v2
it returns false
. I can't figure out how the heck it got that logic. I tested that because v2
contains the value NaN
and no matter what v2 == NaN
is false
. I am trying to make a vector-path calculator program that enables you to insert code that indicates the path that an imaginary "pointer" is supposed to take and return the displacement X and Y values and the total distance traveled.
var pointer = {
x: 0,
y: 0,
length: 0,
move: function(dir,mag){
var rounding = 100;
var direction = dir * (Math.PI / 180);
this.x += Math.round(rounding * (Math.cos(direction) * mag)) / rounding;
this.y += Math.round(rounding * (Math.sin(direction) * mag)) / rounding;
this.length += Number(mag);
document.getElementById("x").innerHTML = this.x;
document.getElementById("y").innerHTML = this.y;
document.getElementById("l").innerHTML = this.length;
print("• vector moves pointer to (" + this.x + "," + this.y + ")");
}
};
var variables = {
get: function(id){
return(this.e[id]);
},
set: function(id,v){
this.e[id] = Number(v);
print("▣ variable '" + id + "' was set to " + Number(v));
},
math: function(id1,op,id2){
id2 = id2 || id1;
var a = "";
if(op == "+"){
this.e[id1] += this.e[id2];
a = "added with";
}else if(op == "-"){
this.e[id1] -= this.e[id2];
a = "subtracted by";
}else if(op == "*"){
this.e[id1] *= this.e[id2];
a = "multiplied by";
}else if(op == "/"){
this.e[id1] /= this.e[id2];
a = "divided by";
}else if(op == "%"){
this.e[id1] %= this.e[id2];
a = "modded by";
}else if(op == "&"){
if(this.e[id1] >= this.e[id2]){
this.e[id1] = this.e[id1] - (this.e[id1] - this.e[id2]);
}else{
this.e[id1] = this.e[id2] + (this.e[id1] - this.e[id2]);
}
a = "bitwise-AND-modified by";
}else if(op == "|"){
this.e[id1] == (this.e[id1] + this.e[id2]) / 2;
a = "bitwise-OR-modified by";
}else if(op == "^"){
this.e[id1] == Math.pow(this.e[id1],this.e[id2]);
a = "powered by";
}else if(op == "$"){
this.e[id1] == Math.log(this.e[id2]) / Math.log(this.e[id1]);
a = "logged by";
}
print("√ variable '" + id1 + "' was " + a + " variable" + id2);
},
e: {}
};
function run(){
pointer.x = 0;
pointer.y = 0;
pointer.length = 0;
document.getElementById("console").innerHTML = "<h2>Console</h2>";
var code = document.getElementById("input").value.split(";");
for(var i = 0; i < code.length; i++){
var fn = code[i].split(" ");
var n = fn[0];
if(n == "vector"){
var v1 = Number(fn[1]);
var v2 = Number(fn[2]);
if(v1 == NaN){
v1 = variables.get(fn[1]) || 0;
}
if(v2 == NaN){
v2 = variables.get(fn[2]) || 0;
}
alert(v2 == v2);
pointer.move(v1,v2);
}
if(n == "var"){
var v = Number(fn[2]);
if(v == NaN){
v = variables.get(fn[2]) || 0;
}
variables.set(fn[1],v);
}
if(n == "calc"){
variables.math(fn[1],fn[2],fn[3]);
}
}
}
window.onerror = function(e){
alert(e);
};
function print(msg){
document.getElementById("console").innerHTML += "<p class='msg'>" + msg + "</p>";
}
.msg{
background: #aaffcc;
}
.msg:hover{
background: #88ffaa;
transform: scale(1.25);
}
<!DOCTYPE html>
<html>
<head>
<title>Vectors Calculator</title>
</head>
<body>
<textarea id="input" placeholder="Input code here"></textarea>
<p><button onclick="run();">Run code</button></p>
<p><b>X displacement:</b> <span id="x">0</span></p>
<p><b>Y displacement:</b> <span id="y">0</span></p>
<p><b>Total distance:</b> <span id="l">0</span></p>
<div style="border: 1px solid #000000;" id="console">
<h2>Console</h2>
</div>
<details>
<summary><h2>Help</h2></summary>
<p>Seperate each line by a <code>;</code> semicolon. Use each command like this: <code>function param1 param2; function param1 param2;</code>. The last function doesn't need a semicolon after it.</p>
<table border="1">
<tr>
<th>Function</th>
<th>Parameters</th>
<th>Description</th>
<th>Example</th>
</tr>
<tr>
<td>vector</td>
<td>direction,magnitude</td>
<td>Moves the pointer <code>magnitude</code> units in the direction of <code>direction</code> degrees. 0 degrees is right-facing.</td>
<td><code>vector 0 10;</code> moves the pointer 10 units to the right (x+=10, y+=0)</td>
</tr>
</table>
</details>
</body>
</html>
Why is there a logic issue that makes a variable claim it is not equal to itself? Did I do something wrong? Is there something wrong with my browser (Google Chrome, most recent version as of 16 Aug 2023)? Is something wrong with the JavaScript language itself? What is going on? And, most importantly, is there a fix or workaround?