I was looking at the source to a website and I saw this strange href attribute in a link and I can't figure out what it's doing. It doesn't look like valid javascript. I thought maybe the colon operator here was being used as a way to access the function property popDocWin in the object javascript but when I tried to emulate that syntax on a test function it wouldn't work.
Exterior Adjustable
javascript: is a psuedo-protocol meaning "run the code following this." You can use it (in a browser context) anywhere a URL is valid. In this case, clicking the link "follows" the link, and the act of following the link runs the JavaScript rather than going anywhere. (This is also how bookmarklets work.) So the javascript: part of that isn't JavaScript, just the part after it.
However, something that looks like that is valid JavaScript: If it weren't in a URL, it would create a labelled statement. The primary use of labelled statements in JavaScript is breaking outer loops from within inner ones, like this:
var x, y;
outer: for (x = 0; x < 5; ++x) {
for (y = 0; y < 5; ++y) {
if (x === 2 && y === 3) {
break outer;
}
snippet.show("x = " + x + ", y = " + y);
}
}
Output:
x = 0, y = 0
x = 0, y = 1
x = 0, y = 2
x = 1, y = 0
Note how the x loop was terminated from within the y loop.
var snippet = {
show: function(msg) {
document.body.insertAdjacentHTML("beforeend", "<p>" + msg + "</p>");
}
};
var x, y;
outer: for (x = 0; x < 3; ++x) {
for (y = 0; y < 3; ++y) {
if (x === 1 && y === 1) {
break outer;
}
snippet.show("x = " + x + ", y = " + y);
}
}
The colon is not before the function call, its after the word javascript!
It is telling your browser it should execute the javasript code after the colon.
Well, href isn't a place where your browser would expect javascript code, so javascript: tells the browser that it should treat whatever is after it as javascript.
Related
function countBy(x, n) {
var z = [];
for (i = 0; i < n; i++) {
z.push(z[i] + x);
}
return z;
}
The results I'm getting from the test code Test.assertSimilar(countBy(1,5), [1,2,3,4,5]) are [null,null,null,null,null]. I assume this is because the z array is initially empty. I've searched for some help on this problem but I can't seem to find any. Thank you!
// z is the variable you're building. `z[i]` does not exist yet.
z.push(z[i] + x);
// You want this..
z.push(i*x);
https://jsfiddle.net/9h911x28/
also, matter of preference, but you probably also want to start i out as 1 instead of 0.
for (i = 1; i <= n; i++)
I cannot see what's happening here, can someone enlighten me?
Why am I not entering the FOR?
Code:
console.log('l:269');
var smallestPrice = itemPriceAll; // start with max
console.log('itemNames.length, itemPriceall: ' + itemNames.length + ' ' + itemPriceAll);
for (var y; y < itemNames.length; y++) {
console.log('Are you fucking kidding me?');
console.log('l:272, itemPrice: ' + itemPrice[y] + 'houseEdge: ' + (itemPriceAll * houseEdge));
if (itemPrice[y] >= (itemPriceAll * houseEdge)) {
console.log('l:274');
if (itemPrice[y] < smallestPrice) {
smallestPrice = itemPrice[y];
keeping = itemId[y];
}
}
}
console.log('l:284');
Output:
l:269
itemNames.length, itemPriceall 23 97
l:284
In your for loop, your condition is y < itemNames.length, but the initial value of y is undefined. Thus, the condition is falsy. If you want to do a numeric comparison, you should initialize y to a numerical value.
I'd suggest var y = 0.
for(var y = 0; y < itemNames.length; y++)
was it; I expected y to be empty and numeric.
Thanks to https://stackoverflow.com/users/4265939/neilsimp1
I'm new to javascript and I'm not sure if there is anything called re-initialization in javascript, so please excuse if there is no such concept.
What happens if a javascript variable is re-initialized?
Ex:
function foo()
{
var x = 10;
.... //do something
.... //do something
.... //do something
var x = 20;
console.log("x: " + x);
}
What would be the value of x here?
x will be 20. var is "hoisted" (more below), which means that the variable gets declared as of the beginning of the function, before any step-by-step code is run. Any initializer on a var statement is actually just an assignment statement.
So your code:
function foo() {
var x = 10;
//do something
var x = 20;
console.log("x: " + x);
}
is treated as though it were like this:
function foo() {
var x;
x = 10;
//do something
x = 20;
console.log("x: " + x);
}
When you call a function, the JavaScript engine does several things before it starts executing the step-by-step code in the question. one of the things it does is look through the code and process any var statements. (Although it's called the var statement, var is more of a declaration.)
It's quite fun, your code could also be written this way:
function foo() {
x = 10;
//do something
x = 20;
console.log("x: " + x);
return;
var x;
}
It would still be valid, and it would do exactly the same thing.
More (on my blog): Poor, misunderstood var
ES6 will introduce let, which behaves differently (amongst other things, it is scoped to the block it's in, whereas var is scoped to the function as a whole even if declared within a block); details on MDN.
The var keyword essentially just determines the scope of the variable and is handled before any of the code is actually executed (look up "hoisting"). Using var twice for the variable is redundant, but doesn't really do anything in particular. In the end you're just assigning 10 to x and later you assign 20 to x; nothing more, nothing less.
You can always run simple test to check that. var means it's this variable is in current scope. So in your example x will be equal to 20
var x = 10;
var x = 20;
var y = 10;
function Test() {
var x = 30;
y = 20;
alert('X in function: ' + x);
alert('Y is global, so it\'s ' + y);
}
Test();
alert('X outside of function: ' + x + ', also Y = ' + y);
I am trying to assign each character in a nonempty string to an empty string (making a copy). Here is my code:
x = "example";
y = "";
for (var i = 0; i < x.length; i++) {
y.charAt(i) = x.charAt(i);
}
alert(y);
However, I get the following error in the console:
ReferenceError: invalid assignment left-hand side
y.charAt(i) = x.charAt(i);
I thought it was possible to grow a non-empty string dynamically (hope i'm using the correct terminology). Any help is appreciated!
The "".charAt(0) in an empty string is not defined. Indeed, "a".charAt(0) is "a".
Why don't you concatenate?
x = "example";
y = "";
//Simplest way:
y += x;
//With characters:
for (var i = 0; i < x.length; i++) {
y += x.charAt(i);
}
charAt(x) returns a string containing a character at the position x of the string this, but does not return a link to that position, thus, it is not equal to C's [] notation which returns a link to a byte where the character is located.
So, the charAt(x)= assignment is not valid as well as "a"="b" isn't, too.
Anyway, you can't do "".charAt(x) no matter what is x, because the string is empty.
Anyway, you can't even do "a".charAt(0)="b", because of the reason described in the first paragraph.
So, the only way to change some character in a string is str[x], which works in most browsers. But you still can't ""[0]", so you should first create a string with the same length as the string you are copying, or use concatenation.
It is possible. This is the right way to go:
x = "example";
y = "";
for (var i = 0; i < x.length; i++) {
y += x.charAt(i);
}
Your approach is not correct, since the charAt(i) method returns the (read-only) value of the char at position i.
Instead, an assignment operator, such as =, stores the value on the right side of the op = into the variable on the left side.
In this case y += x.charAt(i) is equal to y = y + x.charAt(i).
e.g.:
1st iteration:
x = "example";
y = ""
i = 0
x.charAt(i) == x.charAt(0) == "e"
y += x.charAt(i) -> y = y + x.charAt(i) -> y = y + x.charAt(0) -> y = y + "e" -> y = "" + "e" = e
2nd iteration:
x = "example";
y = "e"
i = 1
x.charAt(i) == x.charAt(1) == "x"
y += x.charAt(i) -> y = y + x.charAt(i) -> y = y + x.charAt(1) -> y = y + "x" -> y = "e" + "x"
and so on...
At each step, the char at position i of of the string x is appended to the string y and in the end x === y.
My script is causing the browser to freeze and asking me to stop the script. Using firebug I can see the for loop is endlessly looping and not making any progress. Here's the loop:
for (var x = 1; x < 7; x++) {
var y = x; //to stop the value of x being altered in the concat further down
var questionidd = "mcq_question_id";
console.log("1 = " + questionidd);
var questionid = questionidd.concat(y); // mcq_question_id$ctr the question number
console.log("2 = " + questionid);
var mcqid = form[questionid].value; // the questions id on db
console.log("3 = " + mcqid);
var answerr = "mcq_question";
var answer = answerr.concat(y); // mcq_question$ctr the questions chosen answer
var chosenanswer = form[answer].value; // the answers value
console.log("4 = " + chosenanswer);
var amp = "&";
var equal = "=";
var questionide = questionid.concat(equal); // "mcq_question_id$ctr="
var questionida = amp.concat(questionide); // "&mcq_question_id$ctr="
var answere = amp.concat(answer, equal); // "&mcq_question$ctr="
if (x = 1) {
send.push(questionide, mcqid, answere, chosenanswer);
}
else {
send.push(questionida, mcqid, answere, chosenanswer);
}
}
Update - Fixed! Silly mistakes are the worst
if (x = 1) {
should be
if (x === 1) {
The === operator compares while the assignment operator = assigns. Many people make this mistake. :)
When the first loop runs, it sets x to zero, and does so infinitely until the process is terminated. That's why the loop doesn't stop.
if (x = 1) { should be if (x === 1) {
Consider switching to an IDE that catches simple programming errors like this.
Looks like you should have "if (x == 1)" instead of "if (x = 1)".
Your code repeatedly sets x to the value 1, rather than checking that it is equivalent to 1.