Ad
  • Custom User Avatar

    The table is a mapping between the number of the bowling pin and the index of that pin in the output string: 'I I I I\n I I I \n I I \n I '

    For example, pin #7 is the first character in the string, index 0. Pin #8 is at index 2.

    For each pin to be removed, I replace that index in the output string with a space.

    This version might be a little clearer:

    function bowlingPins(arr) {
    
      // locations is mapping of
      // pin: index
      const locations = {
        // Back row
        7:  0,
        8:  2,
        9:  4,
        10: 6,
        
        4:  9,
        5: 11,
        6: 13,
        
        2: 18,
        3: 20,
        
        1: 27
      }
      
      let pins = 'I I I I\n' +
                 ' I I I \n' +
                 '  I I  \n' +
                 '   I   '
                 
      pins = pins.split('')
      
      for (let x of arr)
        pins.splice(locations[x], 1, ' ')
        
      return pins.join('')
    }