I want to find a value of a numerical variable such that it makes the equation true. Example:
var a = random number;
var b = random number;
function (find var c such that a = b + c;) {
console.log(c);
}
Is it possible to make the computer search for the answer of c? and not such that you undo the equation where c = a - b, but such that as it checks answers it gets closer to the value of c.
I would do z-z, and then that's your number. But that's probably not what you are asking.
Related
When the following code is executed in chrome console
let a, b, c;
b = 2;
a = b+1 = c = 2+3;
It says an error 'Invalid left-hand side assignment' for the last statement.
But when we execute the below-given code it accepts it and does not show any error.
let a, b, c;
a = b = c = 2+3;
assignment '=' is an operator so according to operator precedence in javascript, it should work fine. what do you guys think, what is the problem?
for the first code you would need to do
let a,b,c;
b=2;
a=b+1;
c=5;
doing
a=b=c=2+3
works because you arent altering a value to the left of the last equal
the = operator calculate first the right side and require the left side to be lvalue.
In your second example for every assignment operator the left side is always a variable, so it works fine. but in the first example:
let a, b, c;
b = 2;
a = b+1 = c = 2+3;
you trying to assign 5 into b+1, its simply not a variable.
You just need some parenthesis to make it clear what you mean:
a = 1 + (b = c = 2+3);
I have some function:
function calc_degree(a,b,c,cnt) {
if(cnt==0) {
return a;
}
b = b+c;
a = a-b;
return calc_degree(a,b,c,cnt-1);
}
Shortly, it calcs degree of rotation some cicle, which rotation speed increase smoothly. Function returns summary degrees of the rotation. For example:
calc_degree(0,0,1.5,6*1000/time_out);
//a - start angle; b-value of increasing ratoation degree every tick.
//c-increase value; time_out - interval of rotation.
In this example, function returns summary degrees of rotation by 6 seconds.
So, how can I calc the "c" param, if I know the "a" and "cnt"? I need to get the increase value, knowing the summary degrees of rotation and time/tick. If my "a" value is 2790, I need to decrease it every time by "c" value and the last value of "a" must be zero.
Make it a proper recursion, with indices and all:
b[n] = b[n-1] + c => b[n] = b[0] + n*c
a[n] = a[n-1] - b[n] = a[n-1] - b[0] - n*c
results in
a[n] = a[0] - n*b[0] - n*(n+1)/2 * c
This shows you how to get c if a[0]=b[0]=0.
To get a[n]=b[n]=0, you would first need c=-b[0]/n and then c=-a[n]/(n*(n-1)/2). This only works if in the beginning 2*a[0] == (n-1)*b[0].
I cannot comment yet, so I'll add it here. LutzL answer is correct. The problem is that if you add more than one constraint to the problem (in your case requiring both a and b to go to 0) you are reducing the degrees of freedom of the problem. In your case you cannot make a go to zero smoothly (with both a and b 0) if there's not the relation stated by LutzL in the beginning. You can solve it by adding another degree of smoothness (ex: c[n] = c[n-1] + d.) But then you wont be able to make a, b and c tend to 0 without extra constraints.
I have set a number variable under c. After running it through local storage and a couple functions, the variable has turned into a string. Instead of x adding to c , x adds a digit to c. Can anyone see the problem?
function hi() {
c += x;
document.getElementById("paragraph").textContent = "This is a string" + c;
localStorage.clocal = c;
}
function resetvar() {
c = localStorage.clocal;
}
function bla() {
if (localStorage.getItem("clocal") === "null") {
document.getElementById("parargraph").textContent = "This Works Okay";
} else {
document.getElementById("parargraph").textContent = "This is a string" + localStorage.credits;
}
}
the data put in localStorage always as string.
If you wanna to get as number that you have to parse it
like this
c = parseInt(localStorage.clocal);
That's the nature of JS. You can use parseInt(c, 10) + x or x + 1 * c to over come this.
It's a little bit difficult to follow the flow of these methods, but one glaring issue I see is this line:
c += x
In this situation, you're saying that you want to set c equal to the result of c + x where x is a string, instead of setting x equal to x + c? By making this assignment, you are converting c to a string. Then after that point is doesn't matter what else you do -- it will still be a string unless you re-assign it explicitly as an integer.
I hope I understand your intention correctly.. It is a bit unclear.
I have this block, that pulls the current amount paid on the item from Firebase and the amount being paid at the moment. Then it is supposed to add the two together to make the third variable.
For some reason the code drops the cents off the total.
singleRef.once("value", function(snapshot) {
var a = parseInt(snapshot.val().amounts.paid); // The amount already paid
var b = parseInt(invoice.payment.amount); // The amount being applied
var c = a + b;
console.log(c);
});
Let's say the following is happening:
a = 0;
b = 10.86;
The result in this code will be:
c = 10; // should be 10.86
Let's say the following is happening:
a = 10.00;
b = 10.86;
The result in this code will be:
c = 20; // should be 20.86
It doesn't matter what the cents are, it always rounds to get rid of them. I've tried adding .toFixed('2') to all of the variables, just a and b, and just c. All result in the same no cent totals.
HOW!? I've been trying to do this for the past few hours, it's probably simple but I can't figure it out. It's driving me nuts! I'm using angularjs and firebase.
The function parseInt() is specifically for parsing integers so, if you give it 3.14159, it will give you back 3.
If you want to parse a floating point value, try parseFloat().
For example, the following code:
var a = parseInt("3.141592653589");
var b = parseInt("2.718281828459");
var c = a + b;
alert(c);
var d = parseFloat("3.141592653589");
var e = parseFloat("2.718281828459");
var f = d + e;
alert(f);
will give you two different outputs:
5
5.859874482047999
As others have mentioned, you can't parse dollars and cents with parseInt().
And using floats is a bad idea for anything financial/monetary.
Most financial systems simply store prices/dollar values in cents, you can write a function to format it nicely for users if there is a need to display the values.
function(cents) {
cents = +cents; // unary plus converts cents into a string
return "$" + cents.substring(0, cents.length - 2) + "." + cents.substring(cents.length - 2);
}
I have two variables, 'a' and 'b' in my JavaScript, and i want to add them together - i assume this code:
var a = 10;
var b = 30
var varible = a + b;
This, puts the two numbers next to each other... any ideas why... the result should be 40?
You probably have strings instead of integers. That is your code really is like this:
var a = "10";
var b = "30";
var c = a + b; // "1030"
There are several ways to convert the strings to integers:
a = parseInt(a, 10); // Parse the string
b = b * 1; // Force interpretation as number
new is a reserved word, I'd use something else in any case.
And with a normal variable name of c it worked for me:
var a = 10;
var b = 30
var c = a + b;
alert(c);
did the expected and alerted 40
new is a keyword in JavaScript. you should not use it to declare your variables or functions. change the variable name from new to something else
Are you sure you didn't do this:
var a = '30';
var b = '40';
Here, I show '30' as a string rather than a number, and I would expect the "+" operator to concatenate two strings. Since this is contrived code, you may not be entirely sure where your variables were initially assign or what type they have. You can check it like this:
var a = '30';
var b = '40';
alert( typeof(a) + '\n' + typeof(b) );
If either of those say 'object' or 'string' rather than 'number' this is your problem. One way this might happen that you didn't expect is with an input. Say you have code like this:
<input id="a" value="30" />
<input id="b" value="40" />
<script language="javascript">
var a = document.getElementById('a').value;
var b = document.getElementById('b').value;
</script>
Here, the value of a text input is always a string initially.
If you want to convert a variable to a number first you should use something like myVar - 0 to coerce a numeric operation or the more-formal parseInt() or parseFloat() functions (don't forget the radix parameter for parseInt()). And always check isNaN() on the results.
I'm really surprised that noone has until now suggested the obvious: "Casting" with JavaScript (I set it in quotes, because it is no real casting).
var a = "1"; // string
var b = Number(a); // number
var c = String (b); // string again
a + b; // "11"
b + a; // 2
a + c; // "11"
Now, why is this no real casting? Because you don't create a new variable of type "number" but a new object "Number" and initialize it with something that could be numerical.
One or both is a string. If you get the values from a HTML input or something, they definitely are. Make sure they're both integers by using parseInt:
var newValue = parseInt(a,10) + parseInt(b,10);
Also, 'new' is a keyword. You can't use that for a variable name :)