#Simple javascript 'missing '}' or illegal start of expression' error
1 messages · Page 1 of 1 (latest)
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.
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
maybe the empty else?
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
Idk, I do that from my phone but quickly like that it seems like something is badly closed
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");
/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
why is there a public int actually? xD
SO basically you're doing java in javascript XD
is that valid js?
Not it's not, as far as I know, XD
but he doesnt have error on the website so maybe he just confused the 2 names xD
;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;
}
}
}
<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
So that's the issue
OP asked for JavaScript where in fact they're doing JAVA
XD
And the issue is in the if
;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");
}
}
<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
There's a lot more logic error
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
I see that's funny
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
;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"));
}
}
0
although all the syntax was intended to be in javascript and the public int was automatically there
Javascript doesn't have type definition like that.
Be sure what language to use and try again XD