How to check if args[1] is in a range of numbers - discord.js

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.

Related

F# I cannot make a countdown from 10 to 0

I'm quite new to F# and wanted to attempt to make a simple countdown; however, in the code below, it tells me there is something wrong with the "t..0".
let countdown x =
let mutable t = 10
for t..0 do
t=t-1
I want it to countdown in the terminal from 10 to 0.
There are three issues with your snippet. First, you do not need to decrement the t in a for loop - this happens automatically. Second, if you want a range like t .. 0 to go down, you need to specify -1 as the step. Also, your syntax for a for loop needs to define a variable (and you do not need to do this outside of the loop):
let countdown x =
for t in 10 .. -1 .. 0 do
printfn "Counting: %d" t
Tomas' answer shows the range syntax, but there's also the imperative syntax, analogous to for loops.
for i = 10 downto 1 do
printfn "Counting: %d" i
For counting up, use for i = 0 to 10.
Try this:
var i = 10;
var interval = setInterval( increment, 1000);
function increment(){
i = i - 1;
if(i <= -1) {
return
}
console.log(i);
}
Breakdown:
Where the countdown should start:
var i = 10;
The increment interval: (usually 1000ms)
var interval = setInterval( increment, 1000);
The actual countdown:
function increment(){
i = i - 1;
If counter goes into negative numbers, stop counting:
if(i <= -1) {
return
}
Write result in the console log:
}
console.log(i);
}

how do I define c style code blocks in lua?

is there a way to define {} and blankspace as a lua code block?
something like this..
function()
{
local x = 3
if (x == 1) { print("hi1") }
elseif (x == 2) print("hi2")
else (x == 3) print("hi3")
}
it would also be nice to define things like ++ and += too
Just use do..end. += operator and friends aren't conformant with spirit of Lua. Your code will not run. First of all, you need to understand basic Lua syntax. Example of corrected code:
function f()
local x = 3
if x == 1 then
print("hi1")
elseif x == 2 then
print("hi2")
elseif x == 3 then
print("hi3")
end
end
To create block simply use
do
print('Hello, world!')
end
You can check out Lua manual here, whenever you run to trouble.

Testing intermediate variables in a large file using Frama-c

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.

Find an specific array index

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();
}

Multiple logical operator || (OR) conditions in for loop in C

I started studying C a week ago and decided to write my own tic-tac-toe game for practise.
I have a game loop in main:
for(int i = 1; player1.isWinner!=1 || player2.isWinner!=1 || noWinner!=1; i++){...}
Where i - counts turns and condition of end of the game is one of players has won, or no one has won (draw).
For now, it quits executing only if all conditions are 1.
How can I make it work right?
Is a value of 1 where someone won?
If so, then you would need check any of those conditions is true and loop if they are not:
!(player1.isWinner==1 || player2.isWinner==1 || noWinner==1)
Or using AND, check and loop when none are set:
(player1.isWinner!=1 && player2.isWinner!=1 && noWinner!=1)
Consider extracting the condition to a well-named function in order to aid readability and maintanability:
int hasWinner(/*...*/)
{
return player1.isWinner == 1 || player2.isWinner == 1 || noWinner == 1;
}
It then becomes obvious what the condition should be:
for(int i = 1; !hasWinner(/*...*/); i++){ /*...*/ }
You seem to be using some sort of backwards boolean logic. If 1 represents the boolean value true, then the condition should be
!(player1.isWinner || player2.isWinner || noWinner)
This assumes that you set player1.isWinner to 1 when player1 has won.
It would probably be easier to use bool with values true or false from stdbool.h.

Resources