Quick noob question about variables in JavaScript - javascript

Stupid question,
but if I have code like this:
var x;
var y;
var z = x + y;
And then through the program I update the variables for x and y, but when I use the console to check var z, it gives me NaN. So the program is doing that calculation once at the beginning and not updating Z as it continues. So what's the solution to keep checking Z for the variable changes? Do I have to do a loop with setInterval to keep checking the new updated Z variable? Thanks

Yeah, your comment is right. JavaScript doesn't provide a way to create a live binging.
So what's the solution to keep checking Z for the variable changes?
All depends on what you are trying to do.

What you think about the code:
var x = 1;
var y = 2;
var z = x + y;
console.log("The value of z variable: ", z);
PS. Preferably you should use let:
let x = 1;
let y = 2;
let z = x + y;
console.log("The value of z variable: ", z);

Related

How to write multiple index values instead of just one

My function needs to call a number of index values based on the users input (1-5) based on job duties they want to see and then call from the array the index values, so if the user inputs 3, index values 0,1,2 need to be written. Currently it only writes the one associated with the input not all below that input. This is what I have so far:
function jobduties() {
var x = document.getElementById("input").value;
var y = x-1;
var duties = ["Sales", "Customer Service", "Management", "Driving", "Cleaning"];
var z = " ";
while(x > y){
z += z + duties[y];
y++;
document.getElementById("print").innerHTML= z;
}
}
The problem is that you're setting y = x-1, so if the user enters 3 (x=3), y will be 2 (x-1)... In your while you start with y = 2, you add that to z, and then increase y, so you are only evaluating y = 2, since the next one is y=3, and that doesn't satisfy your condition (x > y)
Try these changes...
change var y = x - 1;
for var y = 0;
And also remove the + where you edit the z variable.
Edit: By the way, I just answer following the method you choosed, but I strongly recommend you to use a FOR loop instead, much easier..

Adds as string instead of number

When I add two variables that I initialize as numbers, JS considers them as a string and concatenates them. In a calculation as follows,
var p1 = new window.TRIGEO.Point(150, 150);
var p2 = new window.TRIGEO.Point(500, 350);
var p3 = new window.TRIGEO.Point(50, 500);
var medicentre = new Point((p1.x+p2.x+p3.x)/3,(p1.y+p2.y+p3.y)/3);
(where Point has x and y as members),medicentre is huge =>( 5016683.33 , 50116833.33 ). I do not want this when the answer is actually =>( 233.33 , 333.33 ).
Is there any way to override this behaviour without making the formula too long, cause I have another one, which is at least three lines long. Is this possible without using parseInt()?
EDIT:
Point object is the following and that's it!
function Point(x, y) {
this.x = x;
this.y = y;
}
TRIGEO is the name of the library I'm writing to visualize all the important points and line segments of a triangle. Sorry for the confusion, I probably should've edited.
whilst Parsint as already answered is technically correct, if you want a shorter formula you can trick the casting by making your values positive (as long as you know they aren't going to have non-numberic values
var num1 = '1111.11';
var num2 = 2222.22;
var n1plus2 = (+num1)+num2
// n1p2 : 3333.33
or both strings:
var num1 = '1111.11';
var num2 = '2222.22';
var n1p2 = (+num1)+(+num2);
// n1p2 : 3333.33
JavaScript doesn't consider them as string. You probably execute an operation in the Point class and convert them.
function A(x, y) {
this.x = x;
this.y = y;
}
var a = new A(1, 1),
b = new A(2, 2),
c = new A(a.x + b.x, a.y + b.y);
alert(c.x + " - " + c.y)
This will output 3 - 3 as expected.

Scope of variables in functions using javascript

Why the y is leaked outside the function as scope in JavaScript are bound to functions.
A detailed explanation would be fruitful.
var x = 0;
function f(){
var x = y = 1; // x is declared locally. y is not!
};
f();
console.log(x, y); // 0, 1
It's just syntax error.
The line var x = y = 1; Means:
Declare local variable x,
y = 1,
Declare global variable y (since it's not declared)
x = y;
Replace
var x = y = 1;
with
var x = 1, y = 1;
or
var x = 1;
var y = 1;
and you will get local variable y
http://www.w3schools.com/js/js_variables.asp
The reason the y variable is global is that in Javascript if you omit the var keyword from an assignment statement, the variable the value is assigned to is declared on the global object.
That means that if you wrote the f() function this way, declaring both x and y with the var keyword:
function f(){
var x, y;
x = y = 1;
};
Then both x and y would be local variables (local x shadowing the global one).
It's bad practice to assign a variable without declaring it (with the var keyword).
New versions of JS will throw an error on this. You can use EC5's strict mode to take advantage of this behavior:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode
so you'd write your function this way:
function f(){
'use strict'
var x, y;
x = y = 1;
};
now if you forget to declare y you get an error yelling at you to do so.

Performing Trigonometry in Javascript

How do I perform the following math problem in Javascript:
Tan(x) = 450/120;
x = 75;
// In my calculator I use tan^-1(450/120) to find the answer
// How do I perform tan^-1 in javascript???
My javascript code is outputting incorrect results:
var x = Math.atan(450/120);
alert(x); // says x = 1.3101939350475555
// Maybe I am meant to do any of the following...
var x = Math.atan(450/120) * (Math.PI/2); // still wrong answer
var x = Math.tan(450/120); // still wrong answer
var x = Math.atan(450/120);
Gives the answer 75 degrees in radians, which is: 1.3101939350475555
To get the correct answer just convert to degrees:
var x = Math.atan(450/120) * (180/Math.PI);
alert(x); // says x is 75.06858282186245

In which order are variables assigned in Javascript?

Apparently this is identical in my Firebug console:
var x = "A", y = x;
x + y === "AA";
and
var x = y, y = "A";
x + y === "AA";
Is this standard ECMAScript behaviour, that the order doesn't play a role in comma-separated var assignments?
Edit: The "mystery" is solved. I tested the first example first, then cleared the console and ran the second. However, at this time, y and x were already defined. If you run the JSFiddle provided by David Thomas you always get an "undefinedA". Case settled.
var x = y; will raise an exception if y is not defined.
However, the window object is the default context for Javascript interpreters embedded in browsers. If you previously issued:
y = "A";
Then you actually assigned "A" to window.y, therefore var x = y; becomes valid and assigns window.y to x.

Categories

Resources