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.
Related
I wrote like below. But how to make it more efficient. Need suggestions!!
For example, given:
X = 10
Y = 85
D = 30
Y is the target, D is the jumpcount , X is the current position .
function solution(X, Y, D) {
for(var i =1; X<=Y; i++){
X = X+D;
if( X >= Y ){
return i;
}
}
}
The above code works fine, but how to write efficiently?
Thanks in advance!
You don't need to simulate jumping, You can just use maths:
(X >= Y) ? 1 : Math.ceil((Y-X)/D)
EDIT: Updated for x >= y according to Patrick Robert's suggestion.
Based on the question (minimal number of jumps required), as I understand, when x==y should be 0 steps, then:
Math.ceil(Math.abs((Y-X)/D))
works when Y<0 and/or X<0
When ran it gives an alert box with undefined written, Can you help me debug this code? I am unable to find the correct error you can check it and please help me. It must give the derivative at point x=10 for f(x)=x^2+1 by using smaller and smaller h till desired accuracy is reached.
function f(x) {
return x * x + 1;
}
var iter = [];
var h = 0;
var ddx = 0;
iter[0] = ((f(10 + h) - f(10)) / h);
function d_dx(p) {
for (i = 1; i < 20; i++) {
iter[i] = ((f(p + h) - f(p)) / h);
if (iter[i] = iter[i - 1]) {
break;
var ddx = iter[i];
} else {
h = h / 2;
}
}
return ddx;
}
console.log(d_dx(10));
Your iter array will be NaN. h will be always 0 and you divide by 0.
if (iter[i] = iter[i - 1]) statement is wrong, you should use === to compare values. You cannot use break in if if you want to loop this code multiple times. There are many mistakes in your code.
var ddx is not reachable after break. That's why it is returning undefined. And one more thing, you are using assignment instead of conditional expression.(correct comparison is - if (iter[i] == iter[i - 1])) )
You have used var ddx out of scope while you are returning the ddx.
I am currently studying if condition statement and it is one of my weakest topic ever. Here in this code below, there are two if conditions and I would like to know, how do we find the output of this? I know how to get the output when there is one if. But what about having two if?
function exercise3(){
var x, y, z;
x = 20;
y = 30;
z = 50;
if ((x - 10) < y) {
if (y - 5 > x) {
alert (z - x);
}
else {
alert (z - 5);
}
}
}
exercise3();
This is referred as Nested If statement. Basically you deal with the most outer block first before the going into the inner block. You would only go into the inner block if the condition in the statement is true.
Your statement condition is true
if ((x - 10) < y) {
...
}
Hence you would proceed to read through.
Take note generally to make it more readable better use If else statements rather than if alone as the execution will proceed to check the next if statement with is actually block one and block two (one by one) bringing about slow execution in long written statements.
Ref: https://www.w3schools.com/js/js_if_else.asp
Cheers, happy learning and happy coding.
That is the same as this, if it helps you understand better. The below snippet is just for example purpose, do not follow that. You need to use nested if conditionals.
function exercise3(){
var x, y, z;
x = 20;
y = 30;
z = 50;
// The first if and nested if from your snippet
if ((x - 10) < y) && (y - 5) > x) {
alert(z - x);
}
// The first if and nested else from your snippet
if ((x - 10) < y && (y - 5) <= x) {
alert (z - 5);
}
}
exercise3();
I make an animation with this movement code:
x += -1
I'm just wondering what the difference is if i write this:
x -= 1
the result is still the same, but before i move any futher, is there are any difference in essence between the two?
Thanks.
x += -1 is shorthand for x = x + -1 while x -= 1 is shorthand for x = x - 1. This will produce the same result as long as x is a javascript Number. But because + can also be used for string concatenation, consider x being the String '5' for example and we will have this situation:
'5' + -1 = '5-1' and '5' - 1 = 4.
So it might be advisable to think twice before choosing which one instead of just blindly using them interchangeably.
What you have there are nothing more than shorthand operators. In the first case, it's an Addition Assignment, in the second case it's a Subtraction Assignment.
So your code x += -1 can be interpreted as follows:
x = x + -1; // which is the same as..
x = x - 1; // which can be rewritten as..
x -= 1;
Mathematically there is no difference. 2 + -1 = 1 which is the same as 2 - 1 = 1
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/