My goal with this code was to introduce typos into all words on a page. The script shuffles the letters inside each word, while keeping the first and last characters intact. However, I encountered an issue with punctuation marks such as ".", "(", ",", ")" that sometimes act as the first or last character of a word.
let words = document.body.innerText.split(" ");
let scrambled = "";
for (var i = 0; i < words.length; i++) {
var word = words[i];
var letters = word.split("");
var first = letters[0];
var last = letters[letters.length - 1];
for (var j = 1; j < letters.length - 1; j++) {
var letter = letters[j];
var index = Math.floor(Math.random() * (letters.length - 2)) + 1;
var temp = letters[index];
letters[index] = letter;
letters[j] = temp;
}
letters[0] = first;
letters[letters.length - 1] = last;
scrambled += letters.join("") + " ";
}
document.body.innerText = scrambled;
I created this code to create typo's within all words on a page. The code scrambles the letters in the interior of the word, however, it leaves the first and last character. But, how do I ignore punctuation like "." "(" "," ")" that acts as the first
and/or last character?