javascriptarraysdo-while

Why did targets.some not return true and prevent repeat of coordinates generated?


Console output was as follows:

Targetting Pattern:  Random About Target
Grid Coordinates:  [ 20 , 10 ]
Salvo Coordinates:
21,12,
21,11,
17,8,
19,7,
21,12,  <<== duplicate
20,12,
20,7,
18,12,
21,8,
22,11   <<== duplicate

Code Extract (1st approach): (using 'some' method)

function firePattern_onTargRand( col, row ){
    var firePattern = 'Random About Target' ;
    var target = [] ;
    var targets = [] ;
    var munitionsCost = 10 ;

    for( i=1 ; i <= munitionsCost ; i++ ){
        do {
            target = [ 
                col + getRandomInteger( 0, 6 ) -3 ,
                row + getRandomInteger( 0, 6 ) -3
            ] ;
        } while( targets.some(element => element === target ) ) ;

        targets.push( target ) ;
    } ;

    console.log( "Targetting Pattern:  " + `${firePattern}` + "\nGrid Coordinates:  [" + ` ${col} , ${row} ` + "]\nSalvo Coordinates:\n" + targets ) ;
    return targets ;
} ;

Code Extract (2nd approach): (direct comparison of array elements in loop)

function firePattern_onTargRand( col, row ){
    var firePattern = 'Random About Target' ;
    var target = [] ;
    var targets = [] ;
    var munitionsCost = 10 ;
    var alreadyTargetted = false ;

    for( i=1 ; i <= munitionsCost ; i++ ){
        do {
            target = [ 
                col + getRandomInteger( 0, 6 ) -3 ,
                row + getRandomInteger( 0, 6 ) -3
            ] ;
            alreadyTargeted = false ;
            for( let i = 0 ; i < targets.length ; i++ ){
                if( targets[i] == target ) {
                    alreadyTargeted = true ;
                } ;
            } ;
        } while( alreadyTargeted === true ) ;
    } ;     
    targets.push( target ) ;

    console.log( "Targetting Pattern:  " + `${firePattern}` + "\nGrid Coordinates:  [" + ` ${col} , ${row} ` + "]\nSalvo Coordinates:\n" + targets ) ;
    return targets ;
} ;

Anyone know why I keep getting duplicates, when every pair of coordinates should be unique?

PS - I am very much a JavaScript novice.


Solution

  • Thank you again, @Solt, for your assistance in directing me to other techniques.

    Here is the logic sequence that did finally work for me:

    function firePattern_onTargRand( col, row ){
        var firePattern = 'Random About Target' ;
        var target = [] ;
        var targets = [] ;
        var munitionsCost = 10 ;
        var alreadyTargeted = false ;
        var enumerateTargets = "" ;
    
        for( i=1 ; i <= munitionsCost ; i++ ){
            do {
                target = [ 
                    col + getRandomInteger( 0, 6 ) -3 ,
                    row + getRandomInteger( 0, 6 ) -3
                ] ;
                alreadyTargetted = false ;
                for( let j = 0 ; j < targets.length ; j++ ){
                    if(
                        targets[j][0] === target[0] &&
                        targets[j][1] === target[1]
                    ) {
                        alreadyTargetted = true ;
                        console.log( "duplicate flagged at " + `${j}` + " vs " + `${i}` ) ;
                    } ;
                } ; 
            } while( alreadyTargetted === true ) ;
    
            targets.push( target ) ;
            enumerateTargets = enumerateTargets + "\n[" + target + "]" ;
            } ;
    
        console.log( "Targetting Pattern:  " + `${firePattern}` + "\nGrid Coordinates:  [" + ` ${col} , ${row} ` + "]\nSalvo Coordinates:\n" + enumerateTargets ) ;
        return targets ;
    } ;
    

    And this is the console log generated:

    console log of generated messages

    One observation I can't avoid making is that there seems to be a high occurrence of duplicates being encountered from what is supposed to be a random generator function!!! Encountering one every now and then, I can understand. But as many as have been observed in a single run of only 10 (20) calls seems to be hard to accept! Is that indicative of an issue with the JavaScript built-in function? But that is for another discussion altogether. :-)