Created
          April 27, 2016 21:28 
        
      - 
      
 - 
        
Save SamGerber-zz/75c1305273b300d00cd7bec40724c229 to your computer and use it in GitHub Desktop.  
    Given various ways to score points, represented by an arbitrary array w, count how many ways you can add up to a score s. Raw
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | function waysToScore(awards, score) { | |
| var ways = new Array(score + 1).fill(0); | |
| // There's one way to score 0 points | |
| ways[0] = 1; | |
| // Loop through the awards | |
| for (var a = 0; a < awards.length; a++){ | |
| // Loop through each integer from the award value up to the score | |
| for (var s = awards[a]; s <= score; s++){ | |
| // Calculate what the score must have been prior to award | |
| let previousScore = ways[s - awards[a]]; | |
| // Add each of the ways of getting previousScore to current ways | |
| ways[s] += previousScore; | |
| } | |
| } | |
| // return the score (or 0 if it is still undefined) | |
| return ways[score]; | |
| } | |
| waysToScore([2, 3, 7, 8], 0); // 1 | |
| waysToScore([2, 3, 7, 8], 1); // 0 | |
| waysToScore([2, 3, 7, 8], 2); // 1 | |
| waysToScore([2, 3, 7, 8], 3); // 1 | |
| waysToScore([2, 3, 7, 8], 4); // 1 | |
| waysToScore([2, 3, 7, 8], 5); // 1 | |
| waysToScore([2, 3, 7, 8], 6); // 2 | |
| waysToScore([2, 3, 7, 8], 7); // 2 | |
| waysToScore([2, 3, 7, 8], 8); // 3 | |
| waysToScore([2, 3, 7, 8], 9); // 3 | |
| waysToScore([2, 3, 7, 8], 26); // 23 | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment