Skip to content

Commit

Permalink
Merge pull request #4 from lipsumtext/4_return_color
Browse files Browse the repository at this point in the history
Add return color functionality
  • Loading branch information
silikeite authored Sep 25, 2024
2 parents fee0816 + 034f4eb commit 0efb318
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/anagram_set.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"sixLetterSet" : [
[
"canter",
"nectar",
"recant",
"trance"
]
]
}
33 changes: 29 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
var checkWord = require('check-word')
words = checkWord('en')
const checkWord = require('check-word')
const words = checkWord('en')

const checkIfWord = function (word) {
//const anagramSet = require('./anagram_set.json')

const checkIfWord = (word) => {
return words.check(word)
}

module.exports = { checkIfWord }
const returnColor = (word, wordSet) => {
if (checkIfWord(word)) {
let result = '' // Green = G, Yellow = Y, Red = R
let charCounter = (w) => {
let count = {}
for (let c of w) {
count[c] = (count[c] || 0) + 1
}
return count
}
let wordCharCount = charCounter(word),
wordSetCharCount = charCounter(wordSet[0])
for (let c of word) {
result += ((wordCharCount[c] && wordSetCharCount[c])
? ((wordCharCount[c] == wordSetCharCount[c]) ? 'G' : 'Y')
: 'R'
)
}
return result
}
return false
}

module.exports = { checkIfWord, returnColor }
13 changes: 13 additions & 0 deletions test/return_color_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var assert = require('assert')

var index = require('../src/index')
var anagramSet = require('../src/anagram_set.json')

describe('#returnColor()', function() {
it('should return a color combination if word is valid', function() {
assert.equal(index.returnColor('stairs', anagramSet['sixLetterSet'][0]), 'RGGRGR')
})
it('should return false if word is invalid', function() {
assert.equal(index.checkIfWord('abcdef'), false)
})
})

0 comments on commit 0efb318

Please sign in to comment.