currently I have an ajax returning specific data from my database, the result could change (for example the first time returns 20 rows, the second one 50, etc.) everything works fine at this point. I have code for handling and showing my results. My code is something like this:
for (var x = 0; x < resultLength; x++){
//Here is the problem, I always want to check every 10 results (10,20,30,40,50,60... etc)
if(x== 10 || x==20)
doSomething();
}
The problem is that I don't know how much results will contain my data, the only thing that I know if that I want to do something every 10 results, the possible solution that I find was to add 10, 20, 30, 40, 50, 60.. etc. but at some point this is going to fail and it's a lot of code for my code. What I want to know is if there is an alternative to do this in a better way.
% function is what you looking for:
if (x % 10 === 0)
If you want to skip x = 0 iteration add x > 0 to your if statement
for (var x = 0; x < resultLength; x++){
if (x > 0 && x % 10 == 0)
doSomething();
}
Related
I tried the following code, the bot doesn't error but the command replies with the true outcome only, even when the args[1] is out of the range.
This is the code I tried
if(Number(-10) << parseInt(args[1]) << Number(10)) {
message.channel.send("yes")
} else {
message.channel.send("no")
}
I also tried using the parseInt function on the values of 10 and -10, but same issue, the bot replies with yes only
Unlike Python, you cannot do 2 comparisons in the same "block" of code.
The code 1 < 2 < 3 is actually doing (1 < 2) < 3 which actually works out as true < 3 and as JS uses false=0, true=1, you are now checking if 1 < 3, not if 2 is.
So, in your case, it will always return true as either false(0) OR true(1) will be less than 10.
Just change it to -10 < parseInt(args[1]) && parseInt(args[1]) < 10 with an undefined/length check before it.
For example:
if (args.length >= 2 && -10 < parseInt(args[1]) && parseInt(args[1]) < 10) {
message.channel.send("yes");
} else {
message.channel.send("no");
}
JS won't throw any error of IndexOutOfRange as other languages like C#, Java.
It'll return undefined instead.
Please below snippet for how it may happen.
If your logic is to check a value in between -10 and 10, you have to change a little bit with an & operator.
Cheers.
I am trying to use Frama-c to check some properties of a C function that I have. The function is quite large and there are some intermediate variables that I need to check. (I am following this and this manual)
My program has the following structure:
There are 15 return statements spread throughout the program.
The variables I need to check are assigned values at several places in the program, depending on the path of the program.
my_function(){
intermediate var 1=xx;
//#assert var 1>some_value;
intermediate var 2=yy;
return var 4;
intermediate var 1=xx;
//#assert var 1>some_value;
return var 4;
intermediate var 2=xx;
intermediate var 1=yy;
//#assert var 1>some_value;
return var 4;
}
Explanation: I need to check certain properties related to var 1, var 2 and var 4. I tried 2 approaches.
use assert whenever var 1 is set as above.
Problem with this was that Frama-C checks only the first assert.
use annotations in the beginning.
/*# requires \valid(var 1);
ensures var 1 > some_value;
*/
In this case, Frama-C returns an error.
Question: How can I check the properties for intermediate problems? Is there a sample program?
*I haven't included my original function as it is very long.
As Virgile has mentioned, your question is not very clear, but I assume you are trying to validate some properties of var1 and var2.
This book provides some nice examples and I think the following should help you.
int abs(int val){
int res;
if(val < 0){
//# assert val < 0 ;
res = - val;
//# assert \at(val, Pre) >= 0 ==> res == val && \at(val, Pre) < 0 ==> res == -val;
} else {
//# assert !(val < 0) ;
res = val;
//# assert \at(val, Pre) >= 0 ==> res == val && \at(val, Pre) < 0 ==> res == -val;
}
return res;
}
The author has used the concept of Hoare triples in this scenario, where you check (assert) a certain property by asserting its requirements (pre-condition) for a property and check if a property holds after the corresponding statements are executed.
Hope this helps.
Given a Ruby array of [5, 20, 13, 100], how can check if any single one of those items is >= 100?
Ultimately wanting it to return true or false.
Seems I could do something like arr.select { |num| num >= 100 } and count them, but seems there'd be a more succinct method or it.
Try [5, 20, 13, 100].any?{|x| x >= 100 }.
From a basic point of view you can do with your Array ary:
a nice old plain for:
ret = false
for i in 0...(ary.size)
if ary[i] >= 100
ret = true
break
end
end
or you can use a while
ret = false
i = 0
while not ret or i < ary.size
ret = ary[i] >= 100
i += 1
end
or you can use an each method on the array:
ret = false
ary.each do |el|
ret = true if el >= 100
end
or you can use any? like in #Philip answer
or you can use the max method, as cleverly suggested by #sagar-pandya (this solution is extremely nice, but requires to traverse the whole array):
ary.max >= 100
or you can map your array on a newer array of boolean (but this requires two loops over two arrays, thus it is not as efficient as other solutions), and then inject the result in a new variable with the result:
bol = ary.map { |el| el >= 100 }
ret = bol.inject { |s, el| s or el }
or you can use directly inject (but this time you have to set initial conditions):
ret = ary.inject(false) { |s, el| s or (el >= 100) }
Chose your weapon :). But if you want to do it right think about (given your specific task):
using a method that traverse the array only once;
using a method that stops when it has found a true condition.
I'm quite new to programming and have recently started in Processing.
In my code, the collide function sets the touch boolean to be true but by arraying it, it only tests true for the final array and not the ones before it. Where am I going wrong here? I hope my question is clear enough.
edit:
Sorry, let me try again.
I guess my problem is finding out how to array the collide function properly. I cant seem to add a [i] for the collide in the array.
At the moment, the code works but it only tests true for the last array and not for the ones before it.
The array code:
for(int i = 0 ; i < lineDiv; i++){
collide(xPts[i], yPts[i], vecPoints.xPos, vecPoints.yPos, myDeflector.Thk, vecPoints.d);
The collide function:
void collide(float pt1x, float pt1y, float pt2x, float pt2y, int size1, int size2){
if (pt1x + size1/2 >= pt2x - size2/2 &&
pt1x - size1/2 <= pt2x + size2/2 &&
pt1y + size1/2 >= pt2y - size2/2 &&
pt1y - size1/2 <= pt2y + size2/2) {
touch = true;
}
else{
touch=false;
}
Your "touch" variable is global. Every time you call the collide() function, it overwrites whatever it was set to before. Perhaps you just want to test if touch is true after calling collide(), then exit the for loop?
Alternatively, you may want to make collide() return the touch boolean, avoiding the global.
It looks like what you want to do is to run through a loop, run the function on that element of the array and return a value if any of them are true. This is my best guess, you might want to edit your question to clarify what you are looking to do. So assuming this:
1) change you method to a function
boolean collide(float pt1x, float pt1y, float pt2x, float pt2y, int size1, int size2){
if (pt1x + size1/2 >= pt2x - size2/2 &&
pt1x - size1/2 <= pt2x + size2/2 &&
pt1y + size1/2 >= pt2y - size2/2 &&
pt1y - size1/2 <= pt2y + size2/2) {
return true;
}
else{
return false;
}
2) change your loop and how you are calling it
touch = false; // if you don't set this to false before the loop, it will be the last value taken
for(int i = 0 ; i < lineDiv; i++){
if (collide(xPts[i], yPts[i], vecPoints.xPos, vecPoints.yPos, myDeflector.Thk, vecPoints.d)) touch = true;
Before the action would be that touch might cycle between true and false as you iterate through the array and in processing (because you would likely draw out the data) it is unlikely that you would want this behavior because you wouldn't be able to do anything with it unless you packed that data in another structure like an array.
So now, the "touch" is set to false and will change to true if any function calls return a true. If all are false, it will stay false.
note: you might consider using either xPts.length() or yPts.length() vs lineDiv. This would reduce the possibility of a array out of bounds exception assuming xPts and yPts have the same # of elements.
I had a hard time trying to word my question properly, so i'm sorry if it seems confusing. Also i'm using the flixel library in flash builder. It may not be that important butcause probably anyone that knows a little more than me or even a little AS3 could probably see what i'm doing wrong.
Anyway, what i'm trying to do is basically create 10 instances of this square object I made. all I have to do is pass it an x an y coordinate to place it and it works. so ive tested if i just do:
var testsquare:Bgsq;
testsquare = new Bgsq(0,0);
add(testsquare);
it works fine and adds a square at 0,0 just like i told it to, but i want to add 10 of them then move the next one that's created 25 px to the right (because each square is 25px)
my problem is that I only ever see 1 square, like it's only making 1 instance of it still.
anyone possibly have an idea what I could be doing wrong?
var counter:int = 0;
var bgsqa:Array = new Array;
for (var ibgs:int = 0; ibgs < 10; ibgs++)
{
bgsqa[counter] = new Bgsq(0,0);
bgsqa[counter].x += 25;
add(bgsqa[counter]);
counter++;
}
There's a lot you're doing wrong here.
First off, you're using a pseudo-iterator (counter) to access array elements through a loop instead of, well, using the iterator (ibgs).
Second, I don't see anything in the array (bgsqa) you're iterating through. It's no wonder you're having problems. Here's what you should do.
var bgsqa:Array = [];
for(var i:int=0;i<10;i++)
{
var bgsq:Bgsq = new Bgsq(i * 25, 0);
add(bgsq);
bgsqa.push(bgsq);
}
That should probably do it if your post is accurate.
for (var ibgs:int = 0; ibgs < 10; ibgs++)
{
bgsqa[counter] = new Bgsq(0,0);
bgsqa[counter].x = counter * 25;
add(bgsqa[counter]);
counter++;
}
They start at 0, so applying += is simply adding 25 to 0. This should do the trick.