I stumbled upon some examples of code and what I doesn't know is this x = y = z;
What does this code do?
Is this some kind of validation in javascript?
It sets every variable to the value of the right-most expression. In this case, x and y will both get the value of z.
It's an easy way to instantiate multiple variables, for example
for (var i = j = 0; i < 10; i++)
will give you a loop with 2 counters initialised - one that auto increments, and one that you can manually handle inside the loop.
No, it simply means "Set x and y to z".
No nothing like validation
its simply assign value of z to y and y to x.
so result is value of x , y and z are same.
Related
I have written the following JavaScript, a personal challenge in this case to write a sine function without the use of any native methods...
f=n=>n<2?1:n*f(n-1)
p=(x,y)=>y--?x*p(x,y):1
w=(c,a)=>c()&&a()&w(c,a)
n=100;z=0; x=Math.PI/4
w(()=>n>-1,()=>{z+=p(-1,n)/f(2*n+1)*p(x,2*n+1),n--})
So when I output the value of z, I get the sine of x (i.e. the sine of Pi/4 radians)
I'd like to wrap this last line into a fat arrow function (i.e. s=x=>...Whatever it is... ) and have it spit out the value of z but my last few attempts have been fraught with failure... That way I can enter any value of X and have the sine of X spat out...
I'd like to keep the f, p and w functions separate in this case... I intend to use them later for other things...
Anyone have any clue as to how to achieve this given what I've got?
Thanks...
Make z a local variable in the s function, and then return it from the function.
f=n=>n<2?1:n*f(n-1);
p=(x,y)=>y--?x*p(x,y):1;
w=(c,a)=>c()&&a()&w(c,a);
s = x => {
let z = 0, n = 100;
w(()=>n>-1,()=>{z+=p(-1,n)/f(2*n+1)*p(x,2*n+1),n--});
return z;
};
console.log(s(Math.PI/4));
Is there a way to implement a "latch" function in Javascript that stores the value of a numeric variable 'x', (which count up constantly, automatically) depending on the state of a second variable 'y' (boolean type, user-interact)? Something like this:
x = // count up constantly,
x1 = store the x value when ->
y = is "false" (EVEN IF it becomes "true" again)
The problem is that when "y" returns to the "true" state, "x1" loses the stored value and becomes "undefined" again, and I need that value to do a math operation (subtraction) later. Does anyone know how to solve this (I´m very noob in Javascript)?
Here the code I reached until now:
var x = // this value change constantly
function latchON() {
var x1;
if (!y) {
x1 = x;
return x1;
}
}
z = latchON() - x // this is operation I need perform, but result "NaN", because x1 is "undefined" when y become "true"...
Sorry if I could not explain very well, and sorry for my bad English too (not my native language).
EDIT - Thanks to everyone who tried to help me. I finally managed to solve it, using cookies to store values (I wasted a week trying before I remembered it :( but later than never...), following some instructions on this page http://www.quirksmode.org/js/cookies.html
Not quite sure what you wanted to do, but how about this?
var x = // this value change constantly
var x1 = 0
function latchON() {
if (!y) {
x1 = x;
}
return x1;
}
I am currently converting a function from javascript to another language. I have little experience with javascript.
I am currently with a doubt regarding the following piece of code:
msi = pii.charCodeAt(Y++) << 24 | pii.charCodeAt(Y++) << 16
The Y++ will increase the variable, every time that it is called (and globally change the variable value)?
Or it is equivalent to just increate a unit, like:
Y+1
Regrads
Y++ will increase the value of Y by one, as used in an expression it provides the original (non-incremented) value of Y. You should note however that your code increments Y twice.
var Y = 1;
var Z = Y++; // Z will be 1 and Y will be 2 !!! after this line is executed
This is not specific for javascript.
The increment operators are somewhat different than your ordinary increments such as Y = Y+1... The post and pre increment operators in JavaScript work same as in C, C++, Java and many other. How do they work actually here is a brief intro:
Z = Y++ means store value of Y to Z and then increment value of Y by 1.
whereas
Z = ++Y means increment value of Y by 1 and afterwards store it to Z (i.e. the updated value)
I'm doing a simple exercise meant to be in JavaScript that consists of turning a binary into its respective decimal value... I feel I'm close to finishing the project but for some reason my for loops wont work after a certain line.
//function is called after the press of a button
function convertToBinary (){
oText= document.all?document.all("inout"):document.getElementById("inout");
//inout represents a textarea
sText= oText.value;
alert(sText);
aText = sText.split(/\n\r|\n|\r/g); //got this line after searching this site for solutions
/*alert(aText[1]);
alert(aText[2]);
alert(aText[3]);*/
aText does have values stored, checked a thousand times, but after this line everything goes down, and i can't seem to find the reason why. Here is the rest of the code copy/pasted from my text editor.
for(y = 0; y < aText.lenght;y++){
alert(aText[y]);
}
iCurrent=0;
aBina= aText[0].split("");
for(var x = aBina.lenght-1; x >= 0; x--){
iCurrent = (iCurrent*2) + parseInt(aBina[x]);
alert(iCurrent);
}
aAsci[0]=iCurrent;
alert(iCurrent); //Alerts 0
}
NOTE: variables and arrays are properly defined in the first few lines of the code, i have used them in a previous function that does the opposite(convert decimals to binary) and that works fine. I was sure to clean each array and variable after the function is done. ("o" is for objects, "s" for strings, "a" for arrays, "i" for int's).
There is no apparent syntax error to be seen. I would appreciate not posting the solution for the exercise unless it is really necessary, i might not be doing it right just yet but I like to think through the algorithms my self :p. My problem is for loops are as if the condition was not met, they are just being skipped. I'm using Opera Next as my web browser and the "inspect element" Console does not show any errors either.
Thank you in advance.
This should be,
for(y = 0; y < aText.length;y++)
and this
for(var x = aBina.length-1; x >= 0; x--){
instead
for(var x = aBina.lenght-1; x >= 0; x--){
for(y = 0; y < aText.lenght;y++)
you mispelled this one, "LENGTH"
I'm studying for an exam on JavaScript at the moment. I've also got a little knowledge of C and Perl so I'm familiar with prefix and postfix operators in all three languages.
I did an online practice exam for it and one mistake I made was in evaluating the following code:
var x = 10;
x += x--;
Now, I thought it would evaluate to 19 because it would be 10 + 10, then subtract 1 to make 9. But the feedback I got was that it was wrong and it actually evaluates to 20. I thought that sounded a bit suspicious so I tested it out in an HTML document, and it came out with 20 again. I then tried the equivalents in C and Perl and both evaluated to 19.
Can anyone explain to me why JavaScript evaluates the answer as 20 when other languages evaluate it to 19? The answer I got from the test wasn't too clear to me:
The increment ++
and decrement -- operators can be placed
either before or after an operand. If the increment or decrement
operator is placed before the operand, the operation occurs immediately.
If the increment or decrement operator is placed after the operand, the
change in the operand's value is not apparent until the next time the
operand is accessed in the program. Thus the expression x += x-- is equivalent to x = x +
10 which evaluates to 20.
Expanding the statement
x += x--;
to the more verbose JS code
x = x + (function(){ var tmp = x; x = x - 1; return tmp; })();
the result makes perfect sense, as it will evaluate to
x = 10 + (function(){ var tmp = 10; x = 10 - 1; return tmp; })();
which is 20. Keep in mind that JS evaluates expressions left-to-right, including compound assignments, ie the value of x is cached before executing x--.
You could also think of it this way: Assuming left-to-right evaluation order, JS parses the assignment as
x := x + x--
whereas Perl will use
x := x-- + x
I don't see any convincing arguments for or against either choice, so it's just bad luck that different languages behave differently.
In C/C++, every variable can only be changed once in every statement (I think the exact terminology is: only once between two code points, but I'm not sure).
If you write
x += x--;
you are changing the value of x twice:
you are decrementing x using the postfix -- operator
you are setting the value of x using the assignment
Although you can write this and the compiler won't complain about it (not sure, you may want to check the different warning levels), the outcome is undefined and can be different in every compiler.
Basically, the value of x is decemented after assignment. This example might make it clearer (run in Firebug console)
var x = y =10;
x += y--;
console.log(x , y); // outputs 20 9
In C, the line
x += x--;
is undefined behaviour. It seems like your particular compiler is treating it like:
oldx = x--;
x = x + oldx
However, the ECMAScript specification does specify op= - and it gets the value of the left-hand-side before evaluating the right-hand-side.
So it would be equivalent to:
oldx = x--;
x = oldx + oldx