I would like to, when x reaches 5, exit out of a document.onkeydown function. I tried this:
var x = 0
document.onkeydown = function() {
console.log("Still running...")
x += 1
if (x >= 5) {
break
}
}
but the console says that it was an illegal break statement. I have also tried using a return statement to exit out of the onkeydown loop, but that hasn't worked either. Do any of you know how to exit out of one of these? If so, I would very much like to know it.
I don't see any problem other than replacing break to return... Your code comes out of funtion when X reaches 5
var x = 0
document.onkeydown = function() {
console.log("Still running...")
x += 1
console.log("x" + x)
if (x >= 5) {
console.log("Existing..." )
return;
}
else{
/** Do your stuff **/
console.log('Working')}
}
Related
thanks for looking into this for me.
The issue I am facing is this:
I am keeping track of a variables value, i want to trigger blocks of code after certain values are reached.
currently I am using a switch statement but I am looking for something dynamic.
Example I have now:
class className{
constructor(param){
this.param = param
switch(param.x){
case 25:
param.y += 0.1;
break;
case 50:
y += 0.1;
break;
case 75:
y += 0.1;
break;
}
}
As you can see i want run a block of code after the value of X increments by 25
to manually code each block becomes tedious
i want the same code to run every time the value of x increments by 25
x has no limit and this increments infinitely
so i was looking for some kind of infinite loop or something
does anyone know what I can use for this particular situation
If i could right it like this:
param.x.forEach(25){
param.y += 0.1;
}
I did try this above but to no avail it did not work lol
how can i do this guys any help please?
You could do the calculation like this (x % 25) == 0
var y = 0;
var x = 0;
function trig() {
x++;
console.log(x)
if ((x % 25) == 0) {
y += 0.1;
console.log(y, x)
}
}
setInterval(() => {
trig()
}, 100)
ON Class
class className {
constructor(param) {
this.param = param
}
inc() {
this.param.x++;
if ((this.param.x % 25) == 0) {
this.param.y += 0.1;
}
}
//you could call the inc() function on you click or activity
}
I believe you can just check if (x % 25 === 0 ){ ..do stuff }
The following code pushes all the points below the limit into data, until getPoint returns null.
while ((x = getPoint()) && x < limit) {
data.push(x)
}
console.log(x, 'x should be undefined here')
In this particular case I use an assignment in a conditional, I know it looks ugly, but the question focuses on x which is not local to the block. I tried to place a let there, but it doesn't work.
Is it possible to restrict the scope of x inside the while statement?
Another working implementation would be this one, but in this case I double the test on x:
do {
let x = getPoint()
if (x && x < limit) {
data.push(x)
}
} while(x && x < limit)
or
while (true) {
let x = getPoint()
if (!x || x >= limit) {
break;
}
data.push(x)
}
or
function* getPointIterator(limit) {
let x = getPoint()
while(x && x < limit) {
yield x;
}
}
data.push(...getPointIterator(limit))
You may consider to change the while loop with a for loop:
var limit = 3;
var r = 2;
var data = [];
function getPoint() {
return r++;
}
for (let x=0; (x = getPoint()) && x < limit;) {
data.push(x)
}
console.log(typeof(x) === 'undefined' ? 'undefined' : x, 'x should be undefined here')
1. Code block {...}
You can use a “bare” code block {…} to isolate variables into a “local scope”.
{
// do some job with local variables that should not be seen outside
let message = "Hello";
alert(message); // Hello
}
alert(message); // Error: message is not defined
For your case:
const limit = 3;
let y = 0;
const getPoint = () => y++;
{
let x = 0;
while ((x = getPoint()) && x < limit) {
console.log(x);
}
}
console.log(x, 'x should be undefined here');
The code outside of the block (or inside another script) doesn’t see variables inside the block, because the block has its own Lexical Environment.
2. IIFE (function {...})
You can use so-called “immediately-invoked function expressions” (abbreviated as IIFE) used for this purpose.
They look like this:
(function() {
let message = "Hello";
alert(message); // Hello
})();
For your case:
const limit = 3;
let y = 0;
const getPoint = () => y++;
(function () {
let x = 0;
while ((x = getPoint()) && x < limit) {
console.log(x);
}
})();
console.log(x, 'x should be undefined here');
Here a Function Expression is created and immediately called. So the code executes right away and has its own private variables.
The Function Expression is wrapped with brackets (function {...}), because when JavaScript meets "function" in the main code flow, it understands it as the start of a Function Declaration.
The problem with this code is when I'm executing the if condition. The condition only works if i am using if (pixel.getx() <=100) but does not work for a var x = pixel.getX() & if (x <= 100). Can someone tell me why?
var image = new SimpleImage (200,200);
print (image);
for (var pixel of image.values())
var x = pixel.getX();
var y = pixel.getY()
if (x <= 100 && y <= 100)
{
pixel.setRed(255);
pixel.setBlue(0);
pixel.setGreen(0);
}
else if (x > 100)
{
pixel.setBlue(255);
pixel.setGreen(0);
pixel.setRed(0);
}
print (image);
your for loop is missing {}.
all it does the way you have it in your example is
executing var x = pixel.getX(); as many times as there are image.values()
if you need to repeat a multi line block of code within a for loop it needs to be inside {}
if you are repeating one statement - you don't need {} - that's why it worked when you had if (pixel.getX() <= 100) {...}
Your for loop is missing the braces { } and that's why its not working.
Modified code,
var image = new SimpleImage (200,200);
print (image);
for (var pixel of image.values()) {
var x = pixel.getX();
var y = pixel.getY()
if (x <= 100 && y <= 100) {
pixel.setRed(255);
pixel.setBlue(0);
pixel.setGreen(0);
} else if (x > 100) {
pixel.setBlue(255);
pixel.setGreen(0);
pixel.setRed(0);
}
print (image);
}
I have an update function which call a process function. Sometimes I need to break the for loop, but to do that I need to communicate from the process function to the loop running in update().
I'm passing a false to detect the break intention in process().
Is this a good way of interrupting the for loop in update()?
var system = {
update: function() {
for (var i = 0 ; i < 10 ; i++) {
var r = this.process(i);
if (r == false) break;
}
},
process: function(i) {
console.log('test ' + i);
if (i == 4) return false;
}
}
system.update();
I don't have my script completed yet or anything so I can't post the code. Basically I need a variable to change and keep going through a function increasing by one until it reaches its destination. Something like:
function one(a) {
var x = a;
var max = 3;
if (a < 3) {
// some code
two(x);
} else {
// function will end here quitting the whole thing and possibly other code
}
}
function two(x) {
var change = x+1;
one(change);
}
It all works how I need it but when I first enter function one how would I make it so when x = a doesn't have a value that it will by default be 0?
something like...
function one(a) {
var x = a;
var max = 3;
if (x = undefined) {
x = 0;
} else {
if (x < 3) {
// some code
two(x);
} else {
// function will end here quitting the whole thing and possibly other code
}
}
}
function two(x) {
var change = x+1;
one(change);
}
Any ideas?
You could do this:
function one(a) {
var x = a || 0;
if (x < 3) {
//debugger;
two(x);
} else {
// function will end here quitting the whole thing and possibly other code
alert('Done');
}
}
function two(x) {
x++;
one(x);
}
one();
FIDDLE
var x = a || 0 means x is a if a can be asserted as true or 0.
x++ means x = x + 1
You can check to see if the variable is defined and send it in the functions argument by using the short hand conditional.
typeof(a)=="undefined" ? 0 : a;
You can change your code to:
function one(a) {
var x = (typeof(a)=="undefined" ? 0 : a);
var max = 3;
if (x < 3) {
// some code
two(x);
} else {
// function will end here quitting the whole thing and possibly other code
return;
}
}
Fiddle: http://jsfiddle.net/gBBL2/
var x = (typeof a === 'undefined') ? 0 : a;
If a is undefined, use 0. Otherwise use a as the value of x.