Operator '<' cannot be applied to types 'string' and 'number' - angular9

I am trying to verify that if the password is less than three, this message will appear on the interface:
password must be at least 5 chars long!
But it shows a red line at the number three,,,
How can I solve the problem?
code:
else if(this.user.employee.password< 3){
this.errorMsg="password must be at least 5 chars long!";
}

You should check the length of the password using String length property as follows.
else if (this.user.employee.password.length < 3){
this.errorMsg = "password must be at least 5 chars long!";
}

Related

Comparing values is returning FALSE even though they are exactly the same - Google App Script [duplicate]

I have 2 identical strings, they appear identical in the debugger (and Logger.log), but when I do string1 === string2 it returns false. How can I debug this?
One of the string is a google drive file name, and one of the string is from a google sheet cell. I'm guessing there's an invisible character in one of the string but I have no way to see it.
Check type of each variable
typeof string1 === typeof string2
Check length of each string
string1.length === string2.length
Loop through each character:
[...string1].every((char,i) => char === string2[i] || console.info(`Unequal character at ${i}`))
Check unicode of each character:
console.log([...string1].map((char,i) => [char, char.codePointAt(0),string2.codePointAt(i)]))

C++ If ("Char" == "value") { do [duplicate]

I have a character array and I'm trying to figure out if it matches a string literal, for example:
char value[] = "yes";
if(value == "yes") {
// code block
} else {
// code block
}
This resulted in the following error: comparison with string literal results in unspecified behavior. I also tried something like:
char value[] = "yes";
if(strcmp(value, "yes")) {
// code block
} else {
// code block
}
This didn't yield any compiler errors but it is not behaving as expected.
Check the documentation for strcmp. Hint: it doesn't return a boolean value.
ETA: == doesn't work in general because cstr1 == cstr2 compares pointers, so that comparison will only be true if cstr1 and cstr2 point to the same memory location, even if they happen to both refer to strings that are lexicographically equal. What you tried (comparing a cstring to a literal, e.g. cstr == "yes") especially won't work, because the standard doesn't require it to. In a reasonable implementation I doubt it would explode, but cstr == "yes" is unlikely to ever succeed, because cstr is unlikely to refer to the address that the string constant "yes" lives in.
std::strcmp returns 0 if strings are equal.
strcmp returns a tri-state value to indicate what the relative order of the two strings are. When making a call like strcmp(a, b), the function returns
a value < 0 when a < b
0 when a == b
a value > 0 when a > b
As the question is tagged with c++, in addition to David Seilers excellent explanation on why strcmp() did not work in your case, I want to point out, that strcmp() does not work on character arrays in general, only on null-terminated character arrays (Source).
In your case, you are assigning a string literal to a character array, which will result in a null-terminated character array automatically, so no problem here. But, if you slice your character array out of e. g. a buffer, it may not be null-terminated. In such cases, it is dangerous to use strcmp() as it will traverse the memory until it finds a null byte ('\0') to form a string.
Another solution to your problem would be (using C++ std::string):
char value[] = "yes";
if (std::string{value} == "yes")) {
// code block
} else {
// code block
}
This will also only work for null-terminated character arrays. If your character array is not null-terminated, tell the std::string constructor how long your character array is:
char value[3] = "yes";
if (std::string{value, 3} == "yes")) {
// code block
} else {
// code block
}

Trying to check the value of a cell and compare it to another value to see if it matches(Google Script)

I am trying to creating a booking system, and as of now I am going to allow my user to insert their name and then check if there is a value. But whenever I try to simply accomplish this by using if( m == 'string) it thinks that all of the empty spaces are strings which results in everything saying booked.
Function checkifBooked(name)
{
var string ='BOOKED';
var string2 ='FREE';
if(typeof name == 'string')
{
return string;
}
else{
return string2;
}
}
When you get the value of an empty cell you get an empty string '', which is still a string. Thus, your if always evaluates to True.
In javascript, an empty string evaluates to false, so you can test the string directly.
try:
if(name)
Or if you prefer to be more explicit, you could check the type of name, then check it's length.
if(typeof(name) == 'string' && name.length > 0)

Checking empty and or missing field value in JSON with DataWeave

Hi I am transforming JSON to JSON with Dataweave in Mulesoft 3.8.4. I am working with the following lines in JSON (part of a larger file)
..
"someField": "12",
"otherField": "5441",
..
I want to format 'someField' to a zero left padded string with a total length of 4 (0012). I also want to concat that value to the otherField that also needs to be left padded but with a total length of 6.
To prevent the padding and concatenating to fail I have to check if the field is present, not empty and it has to be numeric.
I have the following code:
"My Number": someField as :number as :string {format: "0000"} ++ otherField as :number as :string {format: "0000"}
when somefield != null
and someField != ""
and someField is :number
and otherField != null
and otherField != ""
and otherField is :number
otherwise "",
But this fails because 'is :number' returns false because actually the value is a string. Checking something like 'and someField as :number is :number' also fails when the value is empty. What's the best way to check?
Thanx for helping out.
John
Okay I figured it out myself. The part that was mostly in the way was to see if the value was a number. As the node in JSON could be non existing, empty or a text it was difficult to be able to do these tests in one when-otherwise.
What was also a requirement is that there always should be a returning value. So skipping when null was not an option.
Changing the number test into a regex helped. To also make the code more readable I also added some functions that I could also re-use.
The code now looks like this:
%function repeat(char, times) (char ++ repeat(char, times - 1)) when times > 0 otherwise ""
%function pad(field, count) field as :number as :string {format: repeat('0', count)}
%function toNumber(value, count) (pad(value, count)
when value matches /^\d+$/
otherwise "")
...
"My Number" : "" when someField == null
otherwise "" when otherField == null
otherwise toNumber(someField, 4) ++
toNumber(filterUpper(otherField), 6),
...
The repeat function gives a string of n-th repeated characters. The pad function left pads my number converted to string with zero's. In this function I use the repeat function to provide the format with the variable zero's string.
The toNumber is a check that see if we only use numbers.
Hope someone else can also benefit from this.

Laravel - SQL - SQLSTATE[01000]: Warning: 1265 Data truncated for column 'nsfw' at row 1

I'm trying to safe some data in my database and get the following error:
SQLSTATE[01000]: Warning: 1265 Data truncated for column 'nsfw' at row 1
The nsfw column has a 0 as a standart value. Thats my table:
The nsfw column is also in the models $fillable array.
I want to detect if a checkbox is checked. If it is checked, nsfw should be 1. If it's not, nsfw should be 0.
Thats the checkbox HTML code:
<div class="form-group">
<label for="nsfw" class="control-label">NSFW</label>
<br>
<input type="checkbox" name="nsfw">
</div>
And thats the controller code:
if (Input::get('nsfw')) {
$nsfw = true;
} else {
$nsfw = false;
}
// dd($nsfw) gives me true or
//false back, depending if the checkbox is checked or not
$thread = new Thread();
$thread->name = Input::get('thread-name');
$thread->description = Input::get('thread-desc');
$thread->age_min = Input::get('thread-min-age');
$thread->age_max = Input::get('thread-max-age');
if ($nsfw == true) {
$thread->nsfw = 1;
} else {
$thread->nsfw = 0;
}
$thread->save();
Thanks for your help and sorry for my bad english!
Go to config/database.php in the connections.mysql set strict to false, then try to run it again, if you have no errors check if you have the data inserted correctly. If you don't then you are entering the wrong type of data to the mentioned column, so set your code to store values as strings:
if ($nsfw == true) {
$thread->nsfw = '1';
} else {
$thread->nsfw = '0';
}
When strict mode is true and you get errors, it means you have other errors in the code, for example a wrong datatype or a spelling error.
Leo's answer is right. I just want to emphasis, that you should reset the strict mode to true again.
Also, check the database column for spelling mistakes for that, or enum value exists in the col.
The data is truncated because you use a comma (,) when input data, try not to use a comma in the data to be input.
this my error :
SQLSTATE[01000]: Warning: 1265 Data truncated for column 'D01' at row 1 (SQL: insert into mbs (kodepertanyaan, D01, D02, D03, D04, D05, D06, D07) values (drg, 0,7, 0, 0, 0, 0, 0, 0))
I try to input 0,7