Unexpected token ;

Hi,
I am getting unexpected token error. My code is:

var assert = require('assert');
     const path = require("path");
     const fs = require("fs");
module.exports = async function(callback) {
try {
     var ctr =0;
     const keyWordStr = [];
     const files = new Array("A1.sol", "A2.sol", "A3.sol");
     const keyWords = new Array("string1",  "string2", "string3","string4", "string5", "string6", "string7");
     const str = 0;
     for (let i = 0; i < files.length; i++) {   
        const contractPath = path.resolve('/home/zulfi/Truffle_programs/search_opcode/','contracts',files[i]);
        const contractCode2 = fs.readFileSync(contractPath, "utf8");
        var lines = this.contractCode2.split(/\r\n|\n/);
        for(var line = 0; line < lines.length-1; line++){
           keyWordStr[ctr] = (lines[line].search(keyWords[ctr] >= 0 ? lines[line] : false);
           console.log(line + " --> "+ lines[line]);
        }//for (var...)   
        //const selfdestructExists2 = (contractCode2.search("pragma Solidity") >= 0 ? true : false);
        //console.log('selfdestruct exists:', selfdestructExists2); 
     }//for(let..)     

    
        
}//try



  catch(error) {
    console.log(error)
  }

  callback()
}

The complete error message is:
==
> SyntaxError: Unexpected token ;
> ```
> 
> $ truffle exec tool2.js
> Using network 'development'.
> 
> /home/zulfi/Truffle_programs/search_opcode/tool2.js:21
>            keyWordStr[ctr] = (lines[line].search(keyWords[ctr] >= 0 ? lines[line] : false);
>                                                                                           ^
> 
> SyntaxError: Unexpected token ;
>     at new Script (vm.js:83:7)
>     at Object.createScript (vm.js:277:10)
>     at Object.file (/home/zulfi/.nvm/versions/node/v10.23.3/lib/node_modules/truffle/build/webpack:/packages/require/require.js:93:1)
>     at Object.exec (/home/zulfi/.nvm/versions/node/v10.23.3/lib/node_modules/truffle/build/webpack:/packages/require/require.js:121:1)
>     at Promise (internal/util.js:274:30)
>     at new Promise (<anonymous>)
>     at bound exec (internal/util.js:273:12)
>     at Object.run (/home/zulfi/.nvm/versions/node/v10.23.3/lib/node_modules/truffle/build/webpack:/packages/core/lib/commands/exec.js:80:1)
>     at process._tickCallback (internal/process/next_tick.js:68:7)
> Truffle v5.1.67 (core: 5.1.67)
> Node v10.23.3

Somebody please guide me.

Zulfi.

Inside of the second for loop there is a line with two opening parenthesis and one closing parenthesis.

1 Like

Hi@Paul_Wilkins,

I can’t understand what is the error in the following ‘for’ loop:

for(var line = 0; line < lines.length-1; line++){
           keyWordStr[ctr] = (lines[line].search(keyWords[ctr] >= 0 ? lines[line] : false);
           console.log(line + " --> "+ lines[line]);
        }//for (var...)  

Please guide me.

Zulfi.

It can help to break up the ternary into condition, truthy expression, and false expression.

Here is the line with the condition, the truthy expression and falsy expression on separate lines.

        keyWordStr[ctr] = (
            lines[line].search(keyWords[ctr] >= 0
            ? lines[line]
            : false
        );

It’s now easier to tell that the search line needs a closing parenthesis.

Hi @Paul_Wilkins ,

Do you want this thing:

for(var line = 0; line < lines.length-1; line++){
keyWordStr[ctr] = (
            lines[line].search(keyWords[ctr] >= 0}
         ? lines[line]
            : false
        );
        console.log(line + " --> "+ lines[line]);
        }//for (var...) 

now following two are outside inner for i.e.
? lines[line]
: false

Is the above correct?

Zulfi.

No sorry, that’s not suitable. A curly brace doesn’t go there. It’s not a curly brace that you add.

Instead, a closing parenthesis needs to be used to end the search part of the line.

1 Like