Issue
I have a no-breaking hypen JS that replaces certain words that have a normal hypen IF the sum of their length(word count) equals a certain values. My actual method is flawed because the physical lenght of each letter is different, for example:
"mmmm-mmm"
"tttt-ttt"
Both have the word count of 8 but the physical lenght is very different if it is not used the monospace font. The question is how do I process each letter of a matches word based on the score values i assigned them? Lets say
a-m is 2
n-z is 1
max score allowed is 10 having the word "mmmm-mmm" the score should be 14.
.replace(/\b((\w+?)\-)((\w+?)(\-(\w+?))?)\b/g, (_, a,b,c,d,e,f)=>{
const wtot1 = parseInt(a.length);
const wtot2 = parseInt(c.length);
const wtot3 = parseInt(d.length);
const wtot4 = wtot2 - wtot3;
const tott = wtot1 + wtot3 + wtot4;
const ggh = 12
if(wtot4 > 0 && tott < ggh + 1)
return `${b}\‑${d}\‑${f}`;
else
if(tott < ggh + 1 && wtot4 === 0)
return `${b}\‑${d}`;
else
return `${_}`;
})
"tott" is the actual word count of the matches letter while "ggh" is the max word count allowed by me.
This is the the script that used the simple word count as reference. I dont know how to process the letters in the matched word in the way i described.
Solution
i think your can do it with an object
, you just need to determine all letters with there scores and map it regardless physical length. we assumes that object name is lettersScors:
const calScore = (word) => {
let score = 0;
for (let i = 0; i < word.length; i++) {
const letter = word[i].toLowerCase();
score += lettersScors[letter] || 0;
}
return score;
};
after it use this function for all the words and get sum of theme in a variable:
const score1 = calScore(word1);
const score2 = calScore(word2);
const score3 = calScore(word3);
const totalSum = score1+score2+score3;
i hop this works for ya
Answered By - Darigan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.