The Question requires to return a string shuffled but in a specific way :
- We only want to “scramble” each word of the sentence, so the order of the words remain the same
- Each word will retain the position of the first and last letter, and the middle letters will be scrambled. For example, if the original word is “animal”, it should randomize the word to something like “aainml”
- If the word is only one or two characters, it remains unchanged
- At most, each sentence will have 5 words
- At most, each word will have 15 characters
- There will never be more than one space in between each word. So “i love learning code”, should become, "i lvoe lernniag cdoe". How do I do this in React?
import "./styles.css";
export default function App() {
var str = "I am a sentence";
var array = str.match(/("[^"]+"|[^"\s]+)/g); //Resturns the words in an array
console.log(array);
var word = "disant";
var middleWord = "";
for(var i = 1;i<word.length-1;i++){
var letter = word[i];
middleWord = middleWord+letter;
};
console.log("This is the middleWord "+middleWord);
var scrambled = middleWord.split('').sort(function(){return 0.5-Math.random()}).join(''); //Shuffles up the string, need to give it what to shuffle
console.log("This is the scrambled word "+scrambled);
word = word[0] + scrambled + word.charAt(word.length);
console.log("this is the result "+word);
return (
<div className="App">
<h1>hello</h1>
</div>
);
}
This is how far Ive gotten, the only problem with my code is that I cant figure out how to insert the last letter at the end.