Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #34088 > unrolled thread
| Started by | Bart Smissaert <bart.smissaert@gmail.com> |
|---|---|
| First post | 2017-10-01 03:06 -0700 |
| Last post | 2017-10-01 07:33 -0700 |
| Articles | 8 — 3 participants |
Back to article view | Back to comp.lang.javascript
What is wrong with this code? Bart Smissaert <bart.smissaert@gmail.com> - 2017-10-01 03:06 -0700
Re: What is wrong with this code? John G Harris <niam@jghnorth.org.uk.invalid> - 2017-10-01 11:38 +0100
Re: What is wrong with this code? Bart Smissaert <bart.smissaert@gmail.com> - 2017-10-01 03:49 -0700
Re: What is wrong with this code? Bart Smissaert <bart.smissaert@gmail.com> - 2017-10-01 03:58 -0700
Re: What is wrong with this code? Julio Di Egidio <julio@diegidio.name> - 2017-10-01 05:08 -0700
Re: What is wrong with this code? Bart Smissaert <bart.smissaert@gmail.com> - 2017-10-01 06:32 -0700
Re: What is wrong with this code? John G Harris <niam@jghnorth.org.uk.invalid> - 2017-10-01 15:04 +0100
Re: What is wrong with this code? Julio Di Egidio <julio@diegidio.name> - 2017-10-01 07:33 -0700
| From | Bart Smissaert <bart.smissaert@gmail.com> |
|---|---|
| Date | 2017-10-01 03:06 -0700 |
| Subject | What is wrong with this code? |
| Message-ID | <690b7ba0-402a-4dfb-b7f8-faa9ddc49075@googlegroups.com> |
Having trouble getting the right syntax for JScript function.
I have run it through the online JSLint syntax checker, but not sure how
to correct the failings it found.
All is fine up to the very last block of code, so if I leave out that nested
loop I can register my function OK.
This is the JS code:
function JS_CountString(lParamCount, strParam1, lParam2, dParam3)
{
var arrSearchVB = new VBArray(UDF.GetVariantArray(1));
//no string to search so exit
if (arrSearchVB.getItem(0) == -1) {
return 0;
}
var arrFindVB = new VBArray(UDF.GetVariantArray(2));
//no string to search for, so exit
if (arrFindVB.getItem(0) == -1) {
return 0;
}
var arrSearchJS = arrSearchVB.toArray();
var arrFindJS = arrFindVB.toArray();
//note we do not use new here
var UBS = arrSearchJS.length;
var UBF = arrFindJS.length;
var i = 0;
var n = 0;
var lCount = 0;
//error in this code block
for (i = 0; i < UBS - UBF; i++) {
for (n = 0; n < UBF; n++) {
If (arrSearchJS[i + n] != arrFindJS[n]);
{ break; } //no need for else here
If (n == UBF)
{ lCount++; }
}
}
}
return lCount;
}
-------------------------------------------------------------------------------
And these are the failings found by JSLint:
Problem at line 32 character 12: Missing 'new' prefix when invoking a constructor.
If (arrSearchJS[i + n] != arrFindJS[n]);
Problem at line 33 character 14: Expected to see a statement and instead saw a block.
{ break; } //no need for else here
Problem at line 33 character 14: Stopping, unable to continue. (76% scanned).
{ break; } //no need for else here
-------------------------------------------------------------------------------
Any suggestions how this can be fixed?
Thanks in advance for any advice.
RBS
[toc] | [next] | [standalone]
| From | John G Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2017-10-01 11:38 +0100 |
| Message-ID | <3ah1tchuni1vh4dc9rjaglq5549g80dk4k@4ax.com> |
| In reply to | #34088 |
On Sun, 1 Oct 2017 03:06:31 -0700 (PDT), Bart Smissaert
<bart.smissaert@gmail.com> wrote:
<snip>
> //error in this code block
> for (i = 0; i < UBS - UBF; i++) {
> for (n = 0; n < UBF; n++) {
> If (arrSearchJS[i + n] != arrFindJS[n]);
> { break; } //no need for else here
> If (n == UBF)
> { lCount++; }
> }
> }
> }
Re-indenting this gives :
//error in this code block
for (i = 0; i < UBS - UBF; i++)
{
for (n = 0; n < UBF; n++)
{
If (arrSearchJS[i + n] != arrFindJS[n])
;
{ break; } //no need for else here
If (n == UBF)
{ lCount++; }
}
}
} // <== What is this close bracket supposed to close ?
// and why do you have an unconditional break statement ?
<snip>
>Problem at line 33 character 14: Expected to see a statement and instead saw a block.
>
> { break; } //no need for else here
<snip>
Lint isn't making sense here. A block *is* a statement. The Standard
says so.
John
[toc] | [prev] | [next] | [standalone]
| From | Bart Smissaert <bart.smissaert@gmail.com> |
|---|---|
| Date | 2017-10-01 03:49 -0700 |
| Message-ID | <7b235dd5-ebb7-4884-ae99-33827c9c7f0f@googlegroups.com> |
| In reply to | #34089 |
On Sunday, 1 October 2017 11:38:52 UTC+1, John G Harris wrote:
> On Sun, 1 Oct 2017 03:06:31 -0700 (PDT), Bart Smissaert
> <bart.smissaert@gmail.com> wrote:
>
> <snip>
> > //error in this code block
> > for (i = 0; i < UBS - UBF; i++) {
> > for (n = 0; n < UBF; n++) {
> > If (arrSearchJS[i + n] != arrFindJS[n]);
> > { break; } //no need for else here
> > If (n == UBF)
> > { lCount++; }
> > }
> > }
> > }
>
> Re-indenting this gives :
>
> //error in this code block
> for (i = 0; i < UBS - UBF; i++)
> {
> for (n = 0; n < UBF; n++)
> {
> If (arrSearchJS[i + n] != arrFindJS[n])
> ;
> { break; } //no need for else here
> If (n == UBF)
> { lCount++; }
> }
> }
> } // <== What is this close bracket supposed to close ?
> // and why do you have an unconditional break statement ?
>
>
>
> <snip>
> >Problem at line 33 character 14: Expected to see a statement and instead saw a block.
> >
> > { break; } //no need for else here
> <snip>
>
> Lint isn't making sense here. A block *is* a statement. The Standard
> says so.
>
> John
>> wat is this close bracket supposed to close ?
I am not familiar with JS at all and I thought it was matching the the first
opening bracket in this code block.
>> and why do you have an unconditional break statement ?
I thought the condition was the preceding line:
If (arrSearchJS[i + n] != arrFindJS[n])
RBS
[toc] | [prev] | [next] | [standalone]
| From | Bart Smissaert <bart.smissaert@gmail.com> |
|---|---|
| Date | 2017-10-01 03:58 -0700 |
| Message-ID | <a6c15984-2c49-4b21-9f09-634923de1ec1@googlegroups.com> |
| In reply to | #34089 |
On Sunday, 1 October 2017 11:38:52 UTC+1, John G Harris wrote:
> On Sun, 1 Oct 2017 03:06:31 -0700 (PDT), Bart Smissaert
> <bart.smissaert@gmail.com> wrote:
>
> <snip>
> > //error in this code block
> > for (i = 0; i < UBS - UBF; i++) {
> > for (n = 0; n < UBF; n++) {
> > If (arrSearchJS[i + n] != arrFindJS[n]);
> > { break; } //no need for else here
> > If (n == UBF)
> > { lCount++; }
> > }
> > }
> > }
>
> Re-indenting this gives :
>
> //error in this code block
> for (i = 0; i < UBS - UBF; i++)
> {
> for (n = 0; n < UBF; n++)
> {
> If (arrSearchJS[i + n] != arrFindJS[n])
> ;
> { break; } //no need for else here
> If (n == UBF)
> { lCount++; }
> }
> }
> } // <== What is this close bracket supposed to close ?
> // and why do you have an unconditional break statement ?
>
>
>
> <snip>
> >Problem at line 33 character 14: Expected to see a statement and instead saw a block.
> >
> > { break; } //no need for else here
> <snip>
>
> Lint isn't making sense here. A block *is* a statement. The Standard
> says so.
>
> John
Yes, thanks, last closing curly brace was the problem. Should have seen that.
Also there is no need for the semi-colon after:
If (arrSearchJS[i + n] != arrFindJS[n])
But JSLint insisted on that.
Function registers fine now, just see now if it works.
RBS
But
[toc] | [prev] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2017-10-01 05:08 -0700 |
| Message-ID | <acb91e19-012c-40ba-b564-c19351bfe984@googlegroups.com> |
| In reply to | #34088 |
On Sunday, October 1, 2017 at 12:06:41 PM UTC+2, Bart Smissaert wrote: > Any suggestions how this can be fixed? If and else, the keywords, are lower-case... Julio
[toc] | [prev] | [next] | [standalone]
| From | Bart Smissaert <bart.smissaert@gmail.com> |
|---|---|
| Date | 2017-10-01 06:32 -0700 |
| Message-ID | <3f1cfe38-3ccf-4d19-a2cc-5f5c5243f137@googlegroups.com> |
| In reply to | #34092 |
On Sunday, 1 October 2017 13:08:54 UTC+1, Julio Di Egidio wrote: > On Sunday, October 1, 2017 at 12:06:41 PM UTC+2, Bart Smissaert wrote: > > > Any suggestions how this can be fixed? > > If and else, the keywords, are lower-case... > > Julio Ah, OK, thanks. RBS
[toc] | [prev] | [next] | [standalone]
| From | John G Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2017-10-01 15:04 +0100 |
| Message-ID | <ket1tc9tm7hoqm4qdiq8u6418kidm7vlu6@4ax.com> |
| In reply to | #34092 |
On Sun, 1 Oct 2017 05:08:43 -0700 (PDT), Julio Di Egidio <julio@diegidio.name> wrote: >On Sunday, October 1, 2017 at 12:06:41 PM UTC+2, Bart Smissaert wrote: > >> Any suggestions how this can be fixed? > >If and else, the keywords, are lower-case... I didn't notice that, but why didn't JSLint ? John
[toc] | [prev] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2017-10-01 07:33 -0700 |
| Message-ID | <c6983fd9-ea08-49a1-8e27-a21a868d36c4@googlegroups.com> |
| In reply to | #34094 |
On Sunday, October 1, 2017 at 4:04:55 PM UTC+2, John G Harris wrote: > On Sun, 1 Oct 2017 05:08:43 -0700 (PDT), Julio Di Egidio > <julio@diegidio.name> wrote: > > >On Sunday, October 1, 2017 at 12:06:41 PM UTC+2, Bart Smissaert wrote: > > > >> Any suggestions how this can be fixed? > > > >If and else, the keywords, are lower-case... > > I didn't notice that, but why didn't JSLint ? Because JSLint isn't *that* smart: keep in mind that If(something) by the JS parser is simply a function call. Hence all those seemingly weird warnings. Also keep in mind that JSLint is parsing, cross-referencing, then heuristics: that warning about blocks may be slightly imprecise, but it is warning that blocks per se do not make scope in JS so there is probably a programming error lurking there (as indeed there was in our case). But JSLint can be tweeked to one's personal or a team's style: for example, I love leveraging hoisting so I disable the rule about re-declared variables, but mileages vary... Julio
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web