0

I am writing a nested For Loop. Trying to setup a break but doesn't seem to work. Here is what I am trying to do. As an example:

var playersList = sam,tom,jane

var playersDrafted = tom,jane

Sam is not found in playersDrafted, so I want Sam's name to not have a line strike. Tom and Jane are found in the playersDrafted, so I do want there names to have a line strike. The code I've written is returning this:

sam
sam
tom
tom
jane
jane

The result should be:

sam
tom
jane

Any advice would be greatly appreciated.

<div>
  <table align="center">     
    <? for(var i=0;i<playersList.length;i++){ ?>
        here:
        <? for(var y=0;y<playersDrafted.length;y++){ ?>

             <? if(playersList[i]==playersDrafted[y]) { ?> 
                 <tr>
                   <td style="text-decoration: line-through;"><?= 
playersList[i]; ?></td>                    
                </tr> 
                 <? break here; ?>
             <? } ?>              

        <? } ?>
             <tr>
                <td><?= 
playersList[i]; ?></td>                    
             </tr> 
    <? } ?> 

  </table>
</div>
ross
  • 2,684
  • 2
  • 13
  • 22
bigr1822
  • 27
  • 2
  • did you consider using `playersDrafted.indexOf(i) > -1`? – Yaelet Jul 29 '19 at 13:46
  • Why do you need the nested loop? Since the names aren't alphabetical, you can just first render all the names not inside `playersDrafted` and then after that all the names inside `playersDrafted`. So two separate loops, not one loop nested into the other. You can even do that with one loop and `Array.includes()` if you don't mind more array lookups. I would probably create a 3rd array to do less lookups, `availableForDraft`. – Shilly Jul 29 '19 at 13:49

1 Answers1

2

As @Shilly mentioned, in your case you don't really need 2 nested for loops. Not using 2 nested for loops would also reduce the complexity space of your code (if you have very big lists, the difference is not neglectable).

However, as you asked help on using 2 nested for loops I have written this small code in pure Javascript. I added a boolean value notDrafted and if the second loop reaches its end without breaking we print out the name of the player not drafted.

var playersList = ["sam", "tom", "jane"];

var playersDrafted = ["tom", "jane"];

var notDrafted = true;

for (var i = 0; i < playersList.length; i++){
    for (var j = 0; j< playersDrafted.length; j++) {
        if(playersList[i]==playersDrafted[j]) {
            console.log(playersList[i] + "with line");
            notDrafted = false;
        }
    }
    if (notDrafted) {
        console.log(playersList[i]);
    }
    notDrafted = true; // reset to not drafted
}
Eduard Voiculescu
  • 141
  • 1
  • 3
  • 14
  • Thanks everyone for your help, @Eduard, your code works perfectly. Sorry, I am just starting to teach myself javascript. I dont understand why I dont need a nested loop. Can you explain in further detail please? If I were to write a VB.Net app, this is how I would accomplish it. – bigr1822 Jul 29 '19 at 14:37