well this is in node to be exact but, when i do the following conditional statement on a string of a length of zero or above zero, it will only ever do the condition if the string is not 0.
if(string.length == 0)
{
console.log(string.length)
} else {
console.log(string.length)
};
no matter what it wont respond if the string.length is 0. i have also tried:
if(string.length > 0)
{
console.log('greater than zero')
} else {
console.log('zero')
};
but with the same results. no matter what it wont respond if the string.length is zero. i have printed the string.length outside of the if/else statement, and have been able to have it print 0. why wont the conditional statement work in this case? why does it just not respond if the string.length == 0?
Seems to work. Fiddle here Firebug records a 0 if there is nothing in the <div>
i was getting whitespace if the string was 0. i solved the error by doing string.trim()
Related
I'm having difficulties figuring out why the code below doesn't work as expected:
const userInput = prompt("Enter something");
if (userInput) {
console.log("TRUTHY");
} else {
console.log("FALSY");
}
I keep getting "TRUTHY" no matter what I do. I understand the logic of this code and even when running the source file from the class I'm not getting the expected output.
I should get "FALSY" whenever the input is: 0, null, undefined, an empty string or NaN.
What am I doing wrong?
Thank you.
Edit 1: Turns out I was getting ahead of myself. The code should in fact return "TRUTHY" unless you input an empty string.
Which browser are you using? because when I run this code on ms edge, it returns FALSY when I enter nothing. Also, userInput is set to a string type by default, and the string "0" is true as it contains something. You'll have to use parseInt() to convert the value to an integer, though that doesn't look like what you want to do. Consider looking for syntax errors, and check if your browser is up to date.
Since userInput is a string we have to check its length to find out whether it is empty
const userInput = prompt("Enter something");
if (userInput.length !== 0 && userInput == 0 && userInput == null && userInput == NaN) {
console.log("TRUTHY");
} else {
console.log("FALSY");
}
Change that if() to:
if (userInput == true) {
It's because the if(), as you have it, does a strict equality (===), so object types much match.
This is my code,
columnLength = tColumns.length;
if (parseInt(columnLength) ==2) {
tColumns[0].parentNode.insertBefore(tD, tColumns[0].nextSibling);
}
if (parseInt(columnLength) >= 3)
{
tColumns[0].parentNode.insertBefore(tD, tColumns[0].nextSibling);
tColumns[0].parentNode.insertAfter(tD, tColumns[0].nextSibling);
}`
Suppose columnLength is 1.. 1st IF condition is false and its not executing the inside statements.
Even 2nd condition is false since 1 is not greater than equals to 3, but the statements are being executed!
What is wrong with the code?
I'm using Visual Studio IDE to debug, even in immediate window also IF condition returns false as shown below.
ONTOPIC :
I assume your tColumns.length might not have the value you expect it to have. I guess it has the value of undefined which can not be parsed as an integer.
http://jsfiddle.net/FRXkM/1/
OFFTOPIC :
Might not be related to your problem. But parseInt requires a second parameter in conventional ways.
For example:
parseInt("34", 10);
For info on parseInt and its parameters go to http://www.w3schools.com/jsref/jsref_parseint.asp
A couple days ago, on a site that I'm the only author on, I added this code to a script:
if (PowerArray[0][0].length < 1);
{
return false;
}
and everything worked fine. When PowerArray[0][0] was "70", the script ran. When PowerArray was empty, the script didn't run past the above quoted line.
This is no longer true. For the life of me, I can't figure it out. I tested with variants of the code, like below:
if (PowerArray[0][0].length < 1);
{
alert(PowerArray[0][0].length);
return false;
}
and set PowerArray[0][0] = "70". When I run the code, I get an alert with "2" in the text. This is the only place that I have an alert in the script. What's going on here, and how do I fix it?
Note: The expected behavior is, of course, no alert, because "70" has a length of 2, and shouldn't trigger the truth of the if.
Edit: 1) Yes, the False in the first block was a typo. It's been corrected. 2) The expected behavior was for it to stop processing if (and only if) PowerArray[0][0].length was 0. 3) I had previously initialized PowerArray as an empty array, and then copied an array (which had the potential to be empty) into it.
You should remove semicolon from if statement, it terminates your statement there.
And yes, when your PowerArray is empty,
PowerArray[0][0] will throw an undefined error,
So should put a null check for that as well.
when PowerArray is empty PowerArray[0] gives undefined then you will get an error for PowerArray[0][0] saying TypeError: Cannot read property '0' of undefined that is why the script is nor running after that line
if (PowerArray && PowerArray[0] && PowerArray[0][0] && PowerArray[0][0].length < 1)
{
return false;
}
I think the semicolon after the if is the issue.
I would also check if PowerArray is a valid 2D array implementation.
Check this link for ideas How can I create a two dimensional array in JavaScript?
Start by changing it to this if (PowerArray[0][0].length < 1)
try this
if (PowerArray[0]) {
if (PowerArray[0][0].length < 1) {
return False;
}
}
I am trying to simplify the following codes. The codes seem to redundant to me. Are there anyone here can help me out? Thanks a lot!
if(area.regionCode=='0' || area.regionCode==null){
var fakecode=area.region.substring(0, area.region.length - 1);
area.region= fakecode +i;
}
Whenever you think some code is not directly revealing, try giving it a new home with a suitable name:
if (!isValidRegionCode(area.regionCode)) {
...
}
...
function isValidRegionCode(regionCode) {
return area.regionCode != null && area.regionCode != '0';
}
It has more code overall, but makes your intentions clear.
if(parseInt(area.regionCode) > 0) {}
I would recommend explicit condition checks. When using:
if (area.regionCode) { }
Style of logic, one is treating varAny as a boolean value. Therefore, JavaScript will perform an implicit conversion to a boolean value of whatever object type varAny is.
or
if(Boolean(area.regionCode)){
codes here;
}
both will work same
returns false for the following,
null
undefined
0
""
false.
beware returns true for string zero "0" and whitespace " ".
you can also first trim the output so " " issue will be solve
here tutorial How do I trim a string in javascript?
in the #mttrb and #nnnnnn described case you can first convert string to either int or float by parseInt() and parseFloat() check this Converting strings to numbers
I am returning cookies through a Chrome Extension - one of the cookies.name is use_hitbox - so naturally I want to do:
if (cookie.name.indexOf("use_hitbox") > 0) {
alert("FOUND HITBOX COOKIE");
}
The Issue is:
cookie.name.indexOf("use") returns true
cookie.name.indexOf("hitbox") returns true
cookie.name.indexOf("use_hitbox") returns false
Any ideas?
Disclaimer:
This is for use on MY site, nothing malicious...!
EDIT: Cool, this works - but the underlying issue was Chrome Caching my Extensions file
indexOf() will return 0 if the match is at the beginning of the string. -1 indicates no match so try:
if (cookie.name.indexOf("use_hitbox") > -1)
Rather than parsing the returned index as boolean (which as pointed out, will mean an index of 0 parses as False), compare result != -1, which is the return value if the substring is not found.