Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript If condition not working

zak100

Coder
Hi,
I am trying to check the if -condition, but it does not work if the condition is false. Following is my code:
Hi,

I am using isNaN(arg) in a if condition but if the arg is a null value, control enters into if condition. How can I avoid it:

My code is:
const path = require("path");
const fs = require("fs");
module.exports = async function(callback)
{
try {
let keywordStr = '11 = cccc = (bbbb sssss )= ttttttttt("")';
let words = keywordStr.split(' '); //number of lines in file

for (let i = 0; i <words.length; i++) {
word= words
console.log("word =" + word)
//NaN = not a number
if (isNaN(word) === false && word !== null){
console.log("new numeric string = " + word);
}
}
}
catch (error) {
console.log(error)
}
callback();
}

I just want to print "new numeric string = 11" butI am getting null values also:
truffle exec findnumeric.js
Using network 'development'.

word =11
testing2 new numeric string = 11
word ==
word =
testing2 new numeric string =
word =cccc
word ==
word =
testing2 new numeric string =
word =
testing2 new numeric string =
word =
testing2 new numeric string =
word =
testing2 new numeric string =
word =
testing2 new numeric string =
word =
testing2 new numeric string =
word =
testing2 new numeric string =
word =(bbbb
word =sssss
word =)=
word =ttttttttt("")
@lc2530hz:~/Truffle_programs/txorigin_assertion_script_tool$
Somebody please guide me.
Zulfi.
 
Actually your program code works fine for me, if only I change word= words to word= words[i].
Then as expected, in only prints new numeric string = 11.
Actually looking at your output, I presume you have also made that change (after posting the code here).
I'm confused by your issue with null values. You are splitting a string into parts - how can any of the parts ever be null ?
Note, I tested with Node.js, not with Turtle.
 
Last edited by a moderator:
Hi @cbreemer

I have removed the null condition, but I am still getting the same output. Maybe it was a typo that you saw :

Code:
word=words

My code is below:

JavaScript:
const path = require("path");
const fs = require("fs");
module.exports = async function(callback)
{
   try {
      let keywordStr = '11 =  cccc =        (bbbb sssss )= ttttttttt("")';
      let words =   keywordStr.split(' '); //number of lines in file
      
      for (let i = 0; i <words.length; i++) {
         word= words[i]
         console.log("word =" + word)
         //NaN = not a number
         if (isNaN(word) === false ){//if(isNaN(word)==false  && word !== null){
            console.log("testing2 new numeric string = " + word);
         }
      }
    }
    catch (error) {
          console.log(error)
    }
    callback();
}


My output is:
Using network 'development'.

word =11
testing2 new numeric string = 11
word ==
word =
testing2 new numeric string =
word =cccc
word ==
word =
testing2 new numeric string =
word =
testing2 new numeric string =
word =
testing2 new numeric string =
word =
testing2 new numeric string =
word =
testing2 new numeric string =
word =
testing2 new numeric string =
word =
testing2 new numeric string =
word =(bbbb
word =sssss
word =)=
word =ttttttttt("")
@lc2530hz:~/Truffle_programs/txorigin_assertion_script_tool$

Somebody please guide me.

Zulfi.
 
Ah, I see what is happening now. Contrary to what I thought, the split function works character-by-character, and does not skip over multiple occurrences of the delimiter. You get empty strings between adjacent spaces. I don't know ifs there is another split-like function that ignores extra white space, but I could not find one. So we have to filter them out. Now your null test makes some sense to me. But be aware that null and empty are different things ! As I said, no words can be null here, but they can be empty. If you add the correct test, it will work. Like this:
JavaScript:
      keywordStr = '11 =  cccc =        (bbbb sssss )= ttttttttt("")';
      words = keywordStr.split(' ');
    
      for (word of words)
      {
         if ( !isNaN(word) && word != '' )
            console.log(word + " is numeric");
         else
            console.log(word);
      }

Note that I have dispensed with the loop counter, and loop directly though the array using the for..of statement.
Also I have changed isNaN(word) === false to !isNaN(word). Never make things more complicated than they have to be.
 
Last edited by a moderator:
Back
Top Bottom