Last active
October 28, 2025 16:11
-
-
Save adamcee/9fe78264baf10d6bceece511b01cfc89 to your computer and use it in GitHub Desktop.
Codewars: 80s Kids #3 Punky Brewsters Socks
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
| // PROBLEM: https://www.codewars.com/kata/5662292ee7e2da24e900012f/train/javascript | |
| // Codewars 80s Kids #3 Punky Brewsters Socks | |
| function getSocks(name, socks) { | |
| let desiredSocks = []; | |
| const sockColors = {}; | |
| const isPunky = name === 'Punky'; | |
| console.log('name is ', name); | |
| console.log('socks', socks) | |
| for(sock of socks) { | |
| console.log(sock); | |
| // if desiredSocks is empty | |
| // For punky - add sock | |
| if(desiredSocks.length === 0) { | |
| if (isPunky) { | |
| console.log('adding punky sock to desiredSocks', sock) | |
| desiredSocks.push(sock) | |
| } | |
| } | |
| // if sockColors[sock] is undefined, create it and set to 1 | |
| if (!sockColors[sock]) { | |
| sockColors[sock] = 1; | |
| } | |
| // check if we already have 1 sock of the same color | |
| else if (sockColors[sock] === 1) { | |
| // we know we have 2 socks of the same color | |
| console.log(sockColors); | |
| console.log(sock); | |
| console.log(sockColors[sock]) | |
| // this was throwing an error i couldnt figure out, so commented it out. its not necessary to solve the problem. | |
| // sockColors[sock] += 1; | |
| // we have a valid match for Henry | |
| // add the pair of same colored socks to desiredSocks and return desiredSocks | |
| if (!isPunky) { | |
| console.log('name is henry, and he has a match') | |
| desiredSocks = [sock, sock]; | |
| return desiredSocks; | |
| } | |
| } | |
| // checking for punky if we have a different colored sock | |
| if(desiredSocks[0] !== sock) { | |
| if(isPunky) { | |
| desiredSocks.push(sock); | |
| // Punky has his valid pair of socks | |
| console.log('Punky valid pair', desiredSocks) | |
| return desiredSocks; | |
| } | |
| } | |
| } | |
| // only 1 sock in desiredSocks means we didnt find a valid pair | |
| // Ex: ['blue', 'blue', 'blue'] --> ['blue'] for Punky thats invalid | |
| // 0 socks in desiredSocks means we didnt find a valid pair | |
| // Ex: ['red', 'green'] --> [] for Henry | |
| if (desiredSocks.length === 1 || desiredSocks.length === 0) { | |
| console.log('no valid pair returning empty arr') | |
| return []; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment