I'm creating a simple hang man like game. The user can input a "secret sentence" and then the players have to guess it by chosing letters one by one.
Since this game will be used in several languages, I would like to solve all accented character if a player chose the non-accented character. For example:
IL CANE È BELLO.
If the player choses the vowel "e" only the vowels without accent will be solved (the game pares characters with ===). Instead I would like that the game recognizes automatically all chars accented or not (è, é, e...)
I'm about to torture myself writing a function that checks every accented character, but I was wondering if ther might be an easier way.
This is what my funciton look like so far (I just added the è accented vowel)
function isAccentedVowel(chosenChar, currentChar) {
console.log('The user selected this char: ' + chosenChar)
console.log('I\'m paring it to this char: ' + currentChar)
switch (chosenChar) {
case 'E':
if (currentChar === 'È') return true;
}
}
note: the secret sentence and every character are uppercase
I'm creating a simple hang man like game. The user can input a "secret sentence" and then the players have to guess it by chosing letters one by one.
Since this game will be used in several languages, I would like to solve all accented character if a player chose the non-accented character. For example:
IL CANE È BELLO.
If the player choses the vowel "e" only the vowels without accent will be solved (the game pares characters with ===). Instead I would like that the game recognizes automatically all chars accented or not (è, é, e...)
I'm about to torture myself writing a function that checks every accented character, but I was wondering if ther might be an easier way.
This is what my funciton look like so far (I just added the è accented vowel)
function isAccentedVowel(chosenChar, currentChar) {
console.log('The user selected this char: ' + chosenChar)
console.log('I\'m paring it to this char: ' + currentChar)
switch (chosenChar) {
case 'E':
if (currentChar === 'È') return true;
}
}
note: the secret sentence and every character are uppercase
Share Improve this question edited Nov 23, 2019 at 1:28 devamat asked Aug 30, 2019 at 4:54 devamatdevamat 2,5139 gold badges33 silver badges57 bronze badges 3- 1 Maybe look at using .charCodeAt() see: stackoverflow./questions/22624379/… , I suspect that those accented characters will fall between a number range, which you may be able to use. Not sure though. Also check out this stackoverflow./questions/990904/… – Spangle Commented Aug 30, 2019 at 5:01
- 1 Strategy pattern is your friend. You select the strategy based on user locale. This way your code stays clean, instead of having a kilometer long switch statement – Mike Doe Commented Aug 30, 2019 at 5:11
- You can also use lodash.deburr before paring, see here: lodash./docs/4.17.15#deburr – user3025289 Commented Jun 23, 2022 at 6:59
2 Answers
Reset to default 4You can use String#normalize
to pare the accented characters with their normal counterpart.
To perform parison, you need to first get/extract normal character from accented character as below.
string.normalize('NFD')
This will return an array containing two items. You can now pare the first item in the array with normal characters.
function isAccented(char, accentedChar) {
return char === accentedChar.normalize('NFD')[0];
}
console.log(isAccented('E', 'È'));
You'll want to use String.prototype.localeCompare()
with sensitivity
value of "base". This has the added bonus of paring case-insensitively (which I assume you'll also want).
If your characters match, localeCompare()
returns 0
.
Here's an example based on the assumption that you'll want to find the positions of the matching characters (ie, a "hang man" game)
const locale = undefined // set this if it matters, otherwise use default
const options = { sensitivity: 'base' }
const findCharPositions = (str, char) =>
Array.prototype.reduce.call(str, (indexes, c, i) =>
(c.localeCompare(char, locale, options) || indexes.push(i), indexes), [])
const str = 'IL CANE È BELLO'
console.log('Find "l":', findCharPositions(str, 'l'))
console.log('Find "e":', findCharPositions(str, 'e'))