-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from lipsumtext/4_return_color
Add return color functionality
- Loading branch information
Showing
3 changed files
with
52 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"sixLetterSet" : [ | ||
[ | ||
"canter", | ||
"nectar", | ||
"recant", | ||
"trance" | ||
] | ||
] | ||
} |
This file contains 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
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 } |
This file contains 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
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) | ||
}) | ||
}) |