Skip to content

Commit

Permalink
feat: fix triple repeats
Browse files Browse the repository at this point in the history
For example 'arr' + 'rabarbra' should be 'arrabarba'
  • Loading branch information
oskogstad authored and loket committed Sep 1, 2023
1 parent 3ebf683 commit d1dcdc5
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/app.njk
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ Object.defineProperty(Array.prototype, 'shuffle', {
// Stolen from https://stackoverflow.com/a/3291856
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}

// Remove tripple repeated letters, for example "arr" + "ruke" should be "arruke"
String.prototype.removeTriplets = function() {
let result = "";

for(let i=0; i < this.length; ++i) {
const current = this[i];
const isTriplet = this[i+1] === current && this[i+2] === current;
if(!isTriplet) {
result += current;
}
}

return result;
}

// Get n random words from list
Expand All @@ -31,10 +46,10 @@ function getRandomWords(n) {

// Get n random words as unordered list
// TODO: Link to each word
// TODO: Add rules for combining words (i.e. concatenate if three of the same letter in a row)
function getRandomWordAsList(n) {
var word = getRandomWords(n)
.join("")
.removeTriplets()
.capitalize();
return `<ul><li>${word}</li></ul>`;
};
Expand Down

0 comments on commit d1dcdc5

Please sign in to comment.