#Simple javascript 'missing '}' or illegal start of expression' error

1 messages · Page 1 of 1 (latest)

novel yacht
#

I'm unsure of what the error is here, any help would be really appreciated

dense dock
#

Send the code like that instead.

```js
Code here
```

#

Check that all of them have closing one have their opening. Go one by one! You should see it.

novel yacht
#
public int countAbc(String str) {
  if(str.length < 3) {
    return 0;
  } else {
      count = 0;
      for(let i = 0; i <= str.length - 3; i++) {
        if (str.substring(i, i+2) == "abc") || (str.substring(i, i+2) == "aba") {
          count++;
        } else {
        }
      }
      return count;
    }
}
#

I checked that they all have closings after openings, so I'm not sure what's wrong

opaque peak
#

maybe the empty else?

dense dock
#
public int countAbc(String str) {
  if(str.length < 3) { // 5
    return 0;
  } 
  else { // 4
      count = 0;
      for(let i = 0; i <= str.length - 3; i++) { // 3
        if (str.substring(i, i+2) == "abc") || (str.substring(i, i+2) == "aba") { // 2
          count++;
        } // 2 
        else { // 1
        } // 1
      } // 3
      return count;
    } // 4
} // 5
#

If you count them from inside to outside you can see that something is wrong and you can pretty much see where

opaque peak
#

are you sure?

#

all the brackets looks fine to me

dense dock
#

Idk, I do that from my phone but quickly like that it seems like something is badly closed

opaque peak
#

you have 6 opening 6 closing

#

it looks fine xD

#

;compile

public int countAbc(String str) {
  if(str.length < 3) {
    return 0;
  } else {
      count = 0;
      for(let i = 0; i <= str.length - 3; i++) {
        if (str.substring(i, i+2) == "abc") || (str.substring(i, i+2) == "aba") {
          count++;
        } else {
        }
      }
      return count;
    }
}
countAbc("Test");
gentle cairnBOT
#
Program Output
/home/jail/prog.js:1
public int countAbc(String str) {
       ^^^

SyntaxError: Unexpected identifier
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1032:15)
    at Module._compile (node:internal/modu
dense dock
#

Oh

#

Yea that's what I just got

opaque peak
#

why is there a public int actually? xD

dense dock
#

SO basically you're doing java in javascript XD

opaque peak
#

is that valid js?

dense dock
#

Not it's not, as far as I know, XD

opaque peak
#

but he doesnt have error on the website so maybe he just confused the 2 names xD

dense dock
#

;compile

class Example {
    public int countAbc(String str) {
      if(str.length < 3) { 
        return 0;
      } else { 
        count = 0;
        
        for(let i = 0; i <= str.length - 3; i++) { 
          if ((str.substring(i, i+2) == "abc") || (str.substring(i, i+2) == "aba")) { // 2
            count++;
          } else { 
          
          } 
        } 
          
        return count;
      } 
    } 
}
gentle cairnBOT
#
Compiler Output
<source>:6: error: cannot find symbol
if(str.length < 3) {
      ^
  symbol:   variable length
  location: variable str of type String
<source>:10: error: cannot find symbol
count = 0;
^
  symbol:   variable count
  location: class Example
<source>:13: error: cannot find symbol
for(let i = 0;
    ^
  symbol:   class let
  location: class Example
<source>:14: error: cannot find symbol
i <= str.length - 3;
        ^
  symbol:   variable length
  location: variable str of type String
<source>:17: error: cannot find symbol
count++;
^
  symbol:   variable count
  location: class Example
<source>:24: error: cannot find symbol
return count;
       ^
  symbol:   variable count
  location: class Example
6 errors
<source>:6: error: cannot find symbol
if(str.length < 3) {
      ^
  symbol:   variable length
  location: variable str of type String
<source>:10: error: cannot find symbol
count = 0;
^
  symbol:   variable count
  location: class Example
<source>:13: error: cannot find symbol
for(l
dense dock
#

So that's the issue

#

OP asked for JavaScript where in fact they're doing JAVA

#

XD

#

And the issue is in the if

opaque peak
#

;compile

public class Test{
public static int countAbc(String str) {
  if(str.length < 3) { 
    return 0;
  } else { 
    count = 0;
    
    for(let i = 0; i <= str.length - 3; i++) { 
      if ((str.substring(i, i+2) == "abc") || (str.substring(i, i+2) == "aba")) { // 2
        count++;
      } else {
      }
    } 
      
    return count;
  } 
} 
public static void main(String[] args){
  countAbc("Test");
}
}
gentle cairnBOT
#
Compiler Output
<source>:3: error: cannot find symbol
  if(str.length < 3) { 
        ^
  symbol:   variable length
  location: variable str of type String
<source>:6: error: cannot find symbol
    count = 0;
    ^
  symbol:   variable count
  location: class Test
<source>:8: error: cannot find symbol
    for(let i = 0; i <= str.length - 3; i++) { 
        ^
  symbol:   class let
  location: class Test
<source>:8: error: cannot find symbol
    for(let i = 0; i <= str.length - 3; i++) { 
                           ^
  symbol:   variable length
  location: variable str of type String
<source>:10: error: cannot find symbol
        count++;
        ^
  symbol:   variable count
  location: class Test
<source>:15: error: cannot find symbol
    return count;
           ^
  symbol:   variable count
  location: class Test
6 errors
<source>:3: error: cannot find symbol
  if(str.length < 3) { 
        ^
  symbol:   variable length
  location: variable str of type String
<source>:6: error: cannot find symbol
 
dense dock
#

There's a lot more logic error

opaque peak
#

yah lol

#

its like a fusion of java and javascript

dense dock
#

The question before going further would be @novel yacht is your goal to write this in Java or JavaScript*. Those are 2 distinct and unrelated languages

#

Because this code is a nice mixt of java and javascript which will cause a lot of errors

novel yacht
#

so basically the specification for the curriculum is javascript but the college gave a task in java

#

and I just checked the website does indeed want java

#

so let's go with java

dense dock
#

;compile

class Example {
    public static int countAbc(String str) {
        int count = 0;
        
        if(str.length() < 3) { 
            return 0;
        } else { 
            count = 0;
        
            for(int i = 0; i <= str.length() - 3; i++) { 
                if ((str.substring(i, i+2) == "abc") || (str.substring(i, i+2) == "aba")) { // 2
                    count++;
                } else { 
          
                } 
            } 
          
            return count;
        } 
    } 
    
    public static void main(String args[]) {
        System.out.println(countAbc("Hello"));
    }
}
gentle cairnBOT
#
Program Output
0
novel yacht
#

although all the syntax was intended to be in javascript and the public int was automatically there

dense dock
#

Be sure what language to use and try again XD