can two variables operated equate to a constant in JavaScript? - javascript

I need to have two variables, x and y, and an equation involving both that always equals one.
This is a simple example. This example may be easy to define in terms of y, like y = 1/x, but I have another equation I need to use, and it is too hard to define in terms of y.
var x, y;
x*y = 1;
x = Math.random()*20;
console.log(y);
I would get an error message for this, like
Uncaught ReferenceError: Invalid left-hand side in assignment (line 2)
a variable could be defined as x*y, like var z= x*y, but apparently not a constant. Maybe there is some way around this, like defining two variables, one as the constant and the other as the equation and finding some way to relate them? Maybe javascript's looseness will allow for some new technique?
Thanks ahead of time! :)

You can't do this directly. The value of x depends on the value of y. You must describe their dependency.
You can do something like:
var x_y = (function () {
var x = 1, y = 1;
return {
set_x: function (new_x) {
x = new_x;
y = 1 / new_x;
},
set_y: function (new_y) {
y = new_y;
x = 1 / new_y;
},
get_x: function () {
return x;
},
get_y: function () {
return y;
}
};
}());
x_y.set_x(Math.random()*20);
console.log(x_y.get_y());
So essentially we're saying x = 1 / y; and y = 1 / x; and we couple the values: if you change one, the other changes as well.

Related

In JavaScript what is meant by 'a function expression is always a constant value'?

I've been following w3schools tutorials for JavaScript ES6, and after showing some example code for arrow functions:
const x = (x, y) => x * y;
They state the following.
Using const is safer than using var, because a function expression is always constant value.
I don't fully understand what they mean here. Are they referring to the fact that I can't edit the arrow function after the expression? Even if that's the case, how exactly does that make the use of const 'safer' than using var? Would something bad happen if I were to write:
var z = (x, y) => x * y;
z = x => x *= -1;
declaring z as const will make sure the function is not overridden later.
Using var will allow the function z to be overridden, line 2 in below code will update z with the function (x) => x*= -1;
var z = (x, y) => x * y; // this code will be forgotten
z = x => x *= -1; // No Error
If const is used, line 2 will give error as z value cannot be changed
const z = (x, y) => x * y; // Final expression - cannot be changed
z = x => x *= -1; // Error
Well, you won't get any errors by doing this.
var z = (x, y) => x * y;
z = x => x *= -1;
But, as you know, functions are intended to contain reusable code and a function is declared to be called later.
If you use var for function expression, that means it can be changed later and you will lose the function you declared.
You declare a function on line 1, but without using it, you declare another function on line 2 and that means the first line is not necessary at all.
Your code is equivalent to the below code block.
function not_used(x, y) {
return x * y;
}
function used(x) {
return x * -1;
}
So, why declaring a function that you don't use?

How do you add numerical values of variables rather than the numbers in JS?

VERY new, barely understand functions.
Here is an example of my issue:
function getx() {
x = 3;
}
function gety() {
y = 2;
}
getx();
gety();
document.write("The sum of x and y is " + x + y);
OUTPUT: The sum of x and y is 32
I would like to know how I can make it so x + y = 5 instead of 32. Obviously 3 + 2 isn't 32, can someone explain to me how I might output the right answer?
You're concatenating the string with x before the add operation. So, you need to wrap your Math operation with parentheses in order to avoid string concatenation.
function getx() {
x = 3;
}
function gety() {
y = 2;
}
getx();
gety();
document.write("The sum of x and y is " + (x + y));
Your functions getx() and gety() aren’t returning any values because you don’t have a return statement.
By calling the functions the way you do, you are creating two global variables: x and y, and initializing the to 3 and 2, respectively.
You should avoid using global variables in this capacity. Your global variables (or functions) can overwrite window variables (or functions). Any function, including the window object, can overwrite your global variables and functions.
Your variables should be declared with var instead.
Unless specificied, js variables are not strongly typed, And since you are using the concatenation operator before adding the variables together, it sees them as a string and this is why your concatenation is putting 3 and 2 together.
If you change your code to something like this, it should accomplish what you’re trying to achieve.
function getx() {
var x = 3;
return x;
}
function gety() {
var y = 2;
return y;
}
document.write("The sum of x and y is " + (gety() + getx()));

Uncaught ReferenceError: X is not defined - JS parsing limitation?

I have these lines in my code:
var Y = 1;
var Z = X || Y;
Where, in a some cases - X is not defined while Y holds a value.
Though I swear I thought it was working before..
I suddenly get "Uncaught ReferenceError: X is not defined".
Wasn't the || operator meant to support such cases?
It does seem to work for:
var X = X || 1;
When X was never defined before..
Is this due to some sort of JS parsing limitation? I'm curious to know.
Thanks
I suddenly get "Uncaught ReferenceError: X is not defined".
You are trying to read from a variable before you've declared it with var (or a function argument list) or written to it.
Wasn't the || operator meant to support such cases?
No.
It does seem to work for var X = X || 1;
You have a var X so the X variable is declared in that instance.
With var Z = X || Y; you are declaring Z but not X (Y was declared on the previous line).
var X = X || 1; works due to hoisting.
It's actually:
var x;
x = x || 1 // undefined || 1

inline variable assign in javascript

I googled a lot on this and didn't get any useful results, I'm trying to declare a variable inline, in C# it works as follows:
int x, y;
x = 5 + (y = 6) + 7;
which will assign y=6 and x=18 and if used right, allows you to do some crazy things in one line.
And my question is, how to do it in JS? Is it even possible?
The only modification required is switching int to var
var x, y;
x = 5 + (y = 6) + 7;
JS Fiddle: http://jsfiddle.net/RP4Dj/

Javascript Functions using unicode operator signs as names?

I was looking at this: What characters are valid for JavaScript variable names?
And I had a thought, and I can't decide if it is a good idea or not...
Say you have some object, for example a Vector (x, y, z):
Vector = (function() {
function Vector(x, y, z) {
this.x = x != null ? x : 0;
this.y = y != null ? y : 0;
this.z = z != null ? z : 0;
if (isNaN(this.x) || isNaN(this.y) || isNaN(this.z)) {
throw new Error("Vector contains a NaN");
}
}
return Vector;
})();
and you wanted to add some Vectors, possibly with a function like this:
this.add = function(v) {
return new Vector(this.x + v.x, this.y + v.y, this.z + v.z);
};
Would it be awful, or awesome, to declare the addition function as this["\u002b"] = function... (or even just this["+"] = function...), and use it like:
var v1 = new Vector(1, 2, 3)
var v2 = new Vector(2, 3, 4)
var v3 = v1["+"](v2)
Obviously, for "add", theres no actual gain in terms of the code size (I think it might be slighly more readable than v1.add(v2), but something such as "multiply" would be,
var v1 = new Vector(1, 2, 3);
v1["×"](2);
or for other objects something like "integrate" could be:
integrand["∫"](0, 1000)
Thoughts? Is it wonderfully wonderful, or horribly hacky? How reliable are unicode characters across browsers? Would it be safe to use in node.js?

Categories