Nested if statements...or use Switch Case? - switch-statement

I have a large nested if. One portion of the nested if works, however, in the second portion only some of the nested if works. I cannot get the switch case to work.
//Match Scoring //All work, when I flip Poker on top of Match the first and second ifs do not work
if playerMatchCardsArray[0] == playerMatchCardsArray[1] &&
playerMatchCardsArray[1] == playerMatchCardsArray[2]{
} else if playerMatchCardsArray[0] == playerMatchCardsArray[1] ||
playerMatchCardsArray[1] == playerMatchCardsArray[2] ||
playerMatchCardsArray[0] == playerMatchCardsArray[2]{
} else if playerMatchCardsArray[0] != playerMatchCardsArray[1] ||
playerMatchCardsArray[1] != playerMatchCardsArray[2] ||
playerMatchCardsArray[0] != playerMatchCardsArray[2] {
//Poker Scoring //All work, when I flip Poker on top of Match. However the following lines work do not work
// SUITED THREE OF A KIND //does not work
if firstRandomNumber == secondRandomNumber && secondRandomNumber == thirdRandomNumber {
// STRAIGHT FLUSH // works
} else if straightCardsArray[0] == 1 && straightCardsArray[1] == 2 && straightCardsArray[2] == 3 &&
flushFirstRandomNumber == flushSecondRandomNumber &&
flushFirstRandomNumber == flushThirdRandomNumber{
// THREE OF A KIND // does not work
} else if pairFirstRandomNumber == pairSecondRandomNumber &&
pairFirstRandomNumber == pairThirdRandomNumber {
//STRAIGHT FORMULA // works
} else if straightCardsArray[0] == 1 && straightCardsArray[1] == 2 && straightCardsArray[2] == 3 {
// SUITED PAIR // does not work
} else if firstRandomNumber == secondRandomNumber ||
secondRandomNumber == thirdRandomNumber ||
firstRandomNumber == thirdRandomNumber {
// PAIR // does not work
} else if pairFirstRandomNumber == pairSecondRandomNumber ||
pairFirstRandomNumber == pairThirdRandomNumber ||
pairSecondRandomNumber == pairThirdRandomNumber {
// FLUSH // works
} else if flushFirstRandomNumber == flushSecondRandomNumber &&
flushFirstRandomNumber == flushThirdRandomNumber {
Thank you in advance for your time and assistance. It's greatly appreciated!

It was the '}' that was causing the issue.

Related

How can i do a comparison in Groovy script?

I am comparing values in Groovy as follows:
if (creds.username == env.Username && creds.password == env.Password)
{
foundUser = true;
break;
}
It should return foundUser = true , but the issue is the second comparison, ( creds.password == env.Password ) if I take that out it works .
Any ideas as to why this may happen ?

Modifying a Linq query to add another condition

I have a method below pulling specific fields that have null values.
Every record in the table has a status field of 1 or 0. 1 is active and 0 is inactive.
For the query below I only want to pull data for active records i.e status = 1
How can I modify this query to put in the condition for status = 1?
public List<FileModel> GetMyData()
{
var collectedData = _context.MyUploads.AsNoTracking().Where(t =>
t.FirstName == null ||
t.LastName == null ||
t.EmailAddress == null ||
t.Telephone == null)
.Select(x => _mapper.Map<FileModel>(x))
.ToList();
return collectedData;
}
You can chain two Where Linq extension method calls right after one another, like so:
public List<FileModel> GetMyData()
{
var collectedData = _context.MyUploads.AsNoTracking().Where(t =>
t.FirstName == null ||
t.LastName == null ||
t.EmailAddress == null ||
t.Telephone == null)
.Where(t => t.status == 1) // added this line...
.Select(x => _mapper.Map<FileModel>(x))
.ToList();
return collectedData;
}
you should just be able to change the where condition to the below
.Where(t => (t.FirstName == null || t.LastName == null || t.EmailAddress == null || t.Telephone == null) && t.status == 1)
The && means and, so its saying those fields are null and the status equals 1.

NodeJS read query from POSTMAN but API doesn't work

I'm trying to get the min and max values based on queries.
I'm using the following logic,
if(req.query.max_price && req.query.max_price != '' && req.query.max_price != 'any'){
qry.price = { $lte: req.query.max_price};
}
// and
if(req.query.min_price && req.query.min_price != '' && req.query.min_price != 'any'){
qry.price = { $gte: req.query.min_price};
}
but I can't seem to get the right response make a request something like the following,
http://localhost:5000/api/ads/list?offset=0&limit=2&min _price=200&max_price=3000
I think there's something wrong with logic, but I can't seem to pin where the problem is.
you are overwriting the qry.price. It should be,
qry.price = {}
if(req.query.max_price && req.query.max_price != '' && req.query.max_price != 'any'){
qry.price = { ...qry.price, $lte: req.query.max_price};
}
if(req.query.min_price && req.query.min_price != '' && req.query.min_price != 'any'){
qry.price = { ...qry.price, $gte: req.query.min_price};
}

Formatting multiple line statement in vim

In vim a long statements like
if ( (image == null) || (image.getFileHash() == null) || (image.getFileImage() == null) )
gets indented like this when I hit an enter before the second OR
if ( (image == null) || (image.getFileHash() == null)
|| (image.getFileImage() == null) ) {
is there a way to set vim to indent it like
if ( (image == null) || (image.getFileHash() == null)
|| (image.getFileImage() == null) ) {
i.e., aligning it with the column after ( of the first previous line.
With dash-tom-bang's help was able to solve a part of the
problem. However, now the curly brackets are not aligned as expected
if { starts on the same line.
if ( (image == null) || (image.getFileHash() == null) ||
(image.getFileImage() == null) )
{
}
if ( (image == null) || (image.getFileHash() == null) ||
(image.getFileImage() == null) ) {
}
The setting that controls this is cinoptions, see :help cinoptions-values.
Add this to your .vimrc to align with the most local unclosed parenthesis. (You can give extra indent in spaces by giving a different number, including negatives. You can indent N tabstops by using Ns.)
set cinoptions=(0
The default value is 2s, which would indent two tabs past the open paren; hopefully this is what you're seeing. :)

Simple IF statement question

How can I simply the below if statements?
if ( isset(var1) & isset(var2) ) {
if ( (var1 != something1) || (var2 != something2) ) {
// ... code ...
}
}
Seems like this could be condensed to only one IF statement but am not certain if I'd use an AND or OR
Boolean varsAreSets = isset(var1) & isset(var2); // or some other name that indicates what this is doing
Boolean someMeaningfulName = (var1 != something1) || (var2 != something2); // would suggest a meaningful name but don't know what this is accomplishing
if ( varsAreSets && someMeaningfulName ) {
// ... code ...
}
This makes the code very readable and helps you and whoever reads the code understand what these checks are actually doing.
if (isset(var1) && ((var1 != something1) || (var1 != something2)))
// ... code ...
}
You would use an and because you can only get to the // ... code ... part if both if-statements are true.
You can do:
if(isset(var1) && isset(var2) && ( (var1 != something1) || (var1 != something2) ) ){
//..code
}
As a general example:
if( cond1 && cond2 ) {
if( cond3 || cond4) {
// ...code..
}
}
The code will be executed only when both cond1 and cond2 are true and either of cond3 or cond3 is true.
It's a question of in what order your computer interprets boolean logic:
Take for example the following conditions:
A: False
B: True
if you were to write if (A && B) what your computer actually does is think:
Is A true? No.
Well, A and B can't be true because A isn't true. Therefore this statement is false. [computer ignores the rest of the logic]
Because of this, when you evaluate the statement isset(var1) && ( (var1 != something1) || (var1 != something2) ) it first checks isset(var1) and if that's false, then it skips the rest of the condition, just like your double-if statement.
if ( isset(var1) && isset(var2) && ( (var1 != something1) || (var2 != something2) ) ) {
// ... code ...
}
if (isset(var1) && isset(var2) && ((var1 != something1) || (var2 != something2)))
{
// ... code ...
}
Another option:
if (isset(var1) && isset(var2)
&& !(var1 == something1 && var2 == something2)) {
...
I think most of the examples above that have 1 IF may spit out an error if var1 or var2 is NOT set
(isset($var1) && isset($var2)) ? ($var1!='something1' && $var2!='something2') ? $go=TRUE : $go=FALSE : $go = FALSE;
if ($go){
echo 'hello';
}

Resources