From 4bed17f0269037eaf76263e382b00cb3a3007491 Mon Sep 17 00:00:00 2001 From: Jave Paid Date: Mon, 23 Sep 2024 22:23:16 +0800 Subject: [PATCH 1/4] Initialize anagram set file with sample word set --- src/anagram_set.json | 10 ++++++++++ src/index.js | 6 ++++++ 2 files changed, 16 insertions(+) create mode 100644 src/anagram_set.json diff --git a/src/anagram_set.json b/src/anagram_set.json new file mode 100644 index 0000000..b65aa3e --- /dev/null +++ b/src/anagram_set.json @@ -0,0 +1,10 @@ +{ + "sixLetterSet" : [ + [ + "canter", + "nectar", + "recant", + "trance" + ] + ] +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 7dd5721..131d4b4 100644 --- a/src/index.js +++ b/src/index.js @@ -1,8 +1,14 @@ var checkWord = require('check-word') words = checkWord('en') +var anagramSet = require('./anagram_set.json') + const checkIfWord = function (word) { return words.check(word) } +const returnColor = function (word) { + //pass +} + module.exports = { checkIfWord } From 818023b0abd4e514412d210d77559ecf54a8dde3 Mon Sep 17 00:00:00 2001 From: Jave Paid Date: Tue, 24 Sep 2024 23:18:12 +0800 Subject: [PATCH 2/4] Add function that returns corresponding color combination --- src/index.js | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index 131d4b4..60ace87 100644 --- a/src/index.js +++ b/src/index.js @@ -1,14 +1,34 @@ -var checkWord = require('check-word') -words = checkWord('en') +const checkWord = require('check-word') +const words = checkWord('en') -var anagramSet = require('./anagram_set.json') +//const anagramSet = require('./anagram_set.json') -const checkIfWord = function (word) { +const checkIfWord = (word) => { return words.check(word) } -const returnColor = function (word) { - //pass +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 + } else { + return false + } } -module.exports = { checkIfWord } +module.exports = { checkIfWord, returnColor } From e419e1129683a3219a12c81ed4751cd3b1df20d6 Mon Sep 17 00:00:00 2001 From: Jave Paid Date: Tue, 24 Sep 2024 23:25:49 +0800 Subject: [PATCH 3/4] Add unit tests to 'returnColor' function --- test/return_color_test.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 test/return_color_test.js diff --git a/test/return_color_test.js b/test/return_color_test.js new file mode 100644 index 0000000..a0ed442 --- /dev/null +++ b/test/return_color_test.js @@ -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) + }) +}) From 034f4eb3e4528049823a8838f9b33ed3fe447f0e Mon Sep 17 00:00:00 2001 From: JP <55536059+javepaid@users.noreply.github.com> Date: Wed, 25 Sep 2024 00:08:36 +0800 Subject: [PATCH 4/4] =?UTF-8?q?Remove=20unnecessary=20=E2=80=98else?= =?UTF-8?q?=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 60ace87..57dde1d 100644 --- a/src/index.js +++ b/src/index.js @@ -26,9 +26,8 @@ const returnColor = (word, wordSet) => { ) } return result - } else { - return false - } + } + return false } module.exports = { checkIfWord, returnColor }