If Always Returns True

If always returns true

 lang == "Deutsch" || "deutsch"

is wrong

lang == "Deutsch" || lang == "deutsch"

is right

"deutsch" alone returns the address of the string in memory. which is
always not equal to zero. which means true.

a == "hello" || "bob"

means

(a == "hello") || "bob"

regardless of what a == "hello" results in (true or false), false || "bob" becomes false || pointer to "bob". All non-null pointers are true, so this is false || true which is true.

#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
cout << "Language?" << endl;
string lang;
cin >> lang;
if(lang == "Deutsch" || lang == "deutsch")
{
cout << "Hallo Welt!";
}
else
{
return false;
}
if(lang == "English" || lang == "english")
{
cout << "Hello World!";
}
else
{
return false;
}
return 0;
}

If statement always returns true c#

Trying to work out the requirements, I reckon that what you want is:

If the target is Limited(), then fail if either we are larger than the capacity, or if there is not enough space in the target to add our amount

So, that would make the condition:

if (link.target.Limited()
&& (link.target.capacity < link.amount
|| link.target.amount + link.amount > link.target.capacity))

To be more symmetric, by swapping the LHS and RHS of the third expression this can be written as:

if (link.target.Limited()
&& (link.target.capacity < link.amount
|| link.target.capacity < link.target.amount + link.amount))

Now, we can further see that link.target.capacity < link.amount is true if and only if link.target.capacity < link.target.amount + link.amount is also true, so we can simplify to:

if (link.target.Limited()
&& link.target.capacity < link.target.amount + link.amount)

BASH If conditional always returns True

Get rid of the unnecessary [[]]. Your new if statement would look like if ((n%x==0 && n%y==0)).

Function always returning true in Javascript ReactJS

filter returns an array, which is always truthy, even if it's empty. You can simplify this a bit using some instead, which returns true if at least one item matches:

const checkCommentAuthor = (): boolean => {
return comments.some((item: any): boolean => {
if (item.username === localStorage.getItem("username")) {
return true;
} else {
return false;
}
});
};

You also don't need this if/else or return type since this already has a boolean equality check:

const checkCommentAuthor = () => {
return comments.some(
(item: any) => item.username === localStorage.getItem("username")
);
};

Finally, you can simplify the JSX with a ternary:

const checkCommentAuthorResult = () => {
let Result = checkCommentAuthor() ? IsCommentAuthor : IsNotCommentAuthor;
return <Result />;
};

This condition will always return true when I try to check string value in Typescript

If number is 32:
First condition is true. You get true.

If number is 0 or any other number:
First condition is true. You get true.

If number is 33:
First condition is false. Second condition is true. You get true.

This is the scenario for which the error exists. Only in this case will the second condition will ever be evaluated. Now at that point you are basically comparing 33 with 32, two values which are different. TS realizes this and gives you the no overlap error.

It is like doing this

let x = true;
if(Math.random() > 0.5 || x == false){
console.log("hi");
}

What you want is :

  let phoneText = this.phone.toString();
let digit1 = phoneText.charAt(0);
let digit2 = phoneText.charAt(1);
let twoDigits: string = digit1+digit2;
if ( (twoDigits!= "33") && (twoDigits!= "32") ){
alert("Invalid");
}

Java if statement always returns true

So for example if i leave the firstname blank on the form

You are just checking for null, you need to do empty ("") String check also.

It should be something like:

if (firstName != null && !"".equals(firstName.trim()) {


Related Topics



Leave a reply



Submit