inline variable assign in javascript - 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/

Related

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

Get Values from one google sheet enter it onto another sheet with a 4 loop

I have to spreadsheets. I want the program to look at Row A on spreadsheet Ind and see if it is a 1 or 0. if it is a one on the active sheet "return" I want it to grab the date from Row D in spreadsheet "Ind" and post it onto Spreadhseet "return". I can't figure this out and I have it working on VBA in excel.
Any help would be greatly appreciated.
function myFunction() {
X = 5;
Y = 2;
Z = 1;
Count = 4560;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source_sheet = ss.getSheetByName("Ind");
var target_sheet = ss.getSheetByName("Returns");
while (Z < Count){
if (source_sheet.getRange("A" & X) = 1) {
var buydate = source_sheeet.getRange("D" & X).getValues()
target_sheet.getRange("A" & Y) = buydate
target_sheet.getRange("B" & Y) = "Buy"
Y = Y + 1
} else if (source_sheeet.Range("C" & X) = 2) {
var selldate = source_sheeet.Range("D" & X).getvalues()
target_sheet.getRange("A" & Y) = selldate
target_sheet.getRange("B" & Y) = "Sell"
Y = Y + 1
}
X = X + 1
Z = Z + 1
}}
This line:
if (source_sheet.getRange("A" & X) = 1) {
Is using an ampersand, and it should be a plus sign. To concatenate strings in JavaScript, use a plus sign.
Also, source_sheet.getRange() will return a range, not a value, so it's never going to equal 1. You would need to use something like the following:
if (source_sheet.getRange("A" + X.toString()).getValue() === 1) {
And use triple equal signs for an equality check. JavaScript is constantly attempting to coerce variables into the type that seems correct. So, it might convert the number in the variable "X" to a string, but you can also use the toString() method.
getValues() returns a two-dimensional array. Each inner array represent a row. Each element in the inner array represents a cell in a row.
If you only want to get one value, use getValue() (no "s" on the end) instead of getValues().
var buydate = source_sheet.getRange("D" + X.toString()).getValue();
You are trying to set the value by using an equal sign. That won't work. You need to use the setValue() or setValues() method.
target_sheet.getRange("A" + Y.toString()).setValue(buydate);
By not using the var key word in your assignments, the variables automatically become "global" variables.
X = 5;
Y = 2;
Z = 1;
There's no need to make them global variables in this case, I don't think.
var X = 5,
Y = 2,
Z = 1;
You can declare multiple variables all at the same time.

can two variables operated equate to a constant in 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.

The button doesn't work

<body>
<input id="input"></input>
<button id="button" onclick="evaluate()">Submit</button>
<br>
<p id="id"></p>
</body>
<script>
var a = 1;
var b = 100;
var z = Math.floor(Math.random() * (b-a)) + a;
document.getElementById("id").innerHTML = ("Pick a number between 1 and 100. I will try to guess it. I think it's " + z + ".");
var y = document.getElementById("input").value;
function evaluate() {
var y = document.getElementById("input").value;
if (y == 0) {
a = x + 1;
z = (a+b)/2;
if (z%2==1) {
z = z-0.5
}
document.getElementById("id").innerHTML = ("Now I think it's " + z);
stopEvent();
}
if (y == 2) {
b = x - 1;
z = (a+b)/2;
if (z%2==1) {
z = z-0.5;
}
document.getElementById("id").innerHTML = ("Now I think it's " + z);
stopEvent();
}
if (y == 1) {
document.getElementById("id").innerHTML = ("Yay! I'm so smart.");
stopEvent();
}
}
</script>
When I click on the button it doesn't think of another integer, it does nothing. I can't find any typos. This program is supposed to guess your number in 7 guesses. You think of a number between 1 and 100, and it first chooses a random integer 1-100, then you tell it if it's too high or too low, then it resets its range according to what you told it, and it chooses another integer, and another, until it narrows down to 1 integer.
Your variable x is never defined. You jump right into using x in a calculation when it is undefined. What is x supposed to be? Once you define x everything should work.
a = x + 1; // What is x?
If you do not define x then your function evaluate() will break.
evaluate() is a predefined method available, so change the name of your method name if you see a conflict.
Now as mentioned by mwilson, you are using variable x without declaring it with some value.
I guess you have already defined stopEvent() in your code or else you will get an error there also.

Can't figure out a simple mathematical equation

What I would like to do is replace the two if statements with a single mathematical formula. I can't for the life of me figure out how, as mathematics was never my strong point. Any advice would be greatly appreciated.
<script>
var x, y, z;
x = 200;
y = 100;
i = 0;
while(z != y) {
i++;
if (x < y) z = x + i;
if (x > y) z = x - i;
document.write(z + "<br>");
}
</script>
Edit: the real code looks like this. It's not too pretty, was hoping I could shrink it down to two lines.
if (prevposX < newposX) posX = prevposX + animStep_;
if (prevposX > newposX) posX = prevposX - animStep_;
if (prevposY < newposY) posY = prevposY + animStep_;
if (prevposY > newposY) posY = prevposY - animStep_;
Edit:
It has been a while, but I believe the Modulus (%) operator would have helped me. I've now moved on to a library to do graphics for me so I didn't need it in the end.
You can make use of a ternary operator
z = x + ((x<y) ? i : -i)
This assumes that when x >= y then you want to -i. If you want to stick exactly to your original you need a second ternary operator, which gets a bit messy:
z = x + ((x<y) ? i : ((x>y) ? -i : 0))
Frankly, it might be clearer to just keep it on 2 lines with 2 if statements like you already have.

Categories