• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding exact element in a javascript variable

 
pramod talekar
Ranch Hand
Posts: 345
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I need to find an exact element dynamically from a javascript variable.

In the present case, indexOf() method is used.
The problem is that if my variable contains a value like SD06 & SD06(A) e.g. cdSD06xxSD06(A), and if I keep a while loop condition with method,
then, the code in the loop will be executed twice as it contains SD06 twice though I want to execute it just once as SD06 & SD06(A) are completely different from each other.

Please advise.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65122
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't want a loop, why have you used a loop?
 
pramod talekar
Ranch Hand
Posts: 345
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do want to use a loop.
just need a replacement for indexOf() method.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65122
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you only want to find the first occurrence, you don't need the loop. If you don't want to just find the first occurrence, then you need to explain what you want better.
 
pramod talekar
Ranch Hand
Posts: 345
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right.
For the While loop, there should be no loop at all, i.e. it shouldn't exist.
What if they change it to the IF loop?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65122
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer the following question with one of: true or false.

I only want to find the first occurrence of the substring.

If you answered true, then there is no need for any sort of loop. And, by the way, an if statement is not considered a "loop". If the intent is to conditionally execute code if the substring is found, then yes, an if statement is appropriate.
 
pramod talekar
Ranch Hand
Posts: 345
Eclipse IDE Java Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that I do need to use If loop.
sorry for the confusion.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "if" statement is not a loop. There is no such thing as an "if loop".
 
pramod talekar
Ranch Hand
Posts: 345
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, right.

Anyways, I figured out the solution for my problem.

Thanks everyone for the advice.

Cheers..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic