Home>

javaScript let team1 = ["a", "b", "c", "d", "e"]; let team2 = ["a", "b", "c", "d", "e"];    for (let t1 of team1) {    team2.shift ();    for (let t2 of team2) {      console.log (t1 + "vs" + t2);   }; };
The brute force goes well with this, but
From this third line, put team1 array a into variable t1
In the 4th line, delete team2 array a and put b in variable t2 to display console (a vs b)
Next, delete the array b of team2 and repeat the process (a vs c), but if you don't know, put "b" in team1
It's time to repeat. Why not delete from the array a of team2 again from the next time.
team1 ["a" b c d e] =>team2 [b c d e]
Isn't team1 [a "b" c d e] =>team 2 [b c d e]?

Thanks for your understanding.

  • Answer # 1

    Array.prototype.shift ()

    Array.prototype.shift ()isdestructive.

    Array.prototype.shift ()-JavaScript | MDN

    breakpoint

    Using the debugger'sbreakpoint, you can find the variable values ​​at key points.

    Pause code at breakpoints | Tools for Web Developers | Google Developers

    Edit question text

    For those who will be consulting later, even if the problem is solved, please respond to [Request to add/modify questions].

    Re: mercury8128

  • Answer # 2

      

    Why don't you delete from the array a of team2 again from the next time?

    About only

    shift handles the array destructively
    2nd lap of team1: When turning with the pattern of t = "b", it is not already team2's original team2
    [b c d e], so remove the leading b and set it to [c d e]
    Rotating with for to of gives b = c, b = d, b = e combinations

    Similarly, on the third lap, team2 is [c d e], so remove the leading c and take [d e]

Trends