0

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.

  • 2
    SO isn't a code writing service, please include a [Minimal, Complete, and Reproducible Code Example](https://stackoverflow.com/help/minimal-reproducible-example) for your code and add any details about any errors or anything that isn't working as expected. Seems a simple string split to get an array of words and then scrambling the words would do the trick. – Drew Reese May 21 '21 at 20:00
  • @mhodges It's literally applying a shuffle algorithm. Google makes finding one trivial, Fischer-Yates is likely the top result (also the top answer from your link ). – Drew Reese May 21 '21 at 20:34
  • Thank you for the answer, I know how to scramble, the issue with my code is that the result string does not include the last letter of my word. And I cant seem to figure out a way to insert it. – Disant Upadhyay May 21 '21 at 20:38

1 Answers1

0

You've an off-by-one error when trying to grab the last letter. Since indices are 0-indexed you want length - 1 to access the last letter.

word = word[0] + scrambled + word.charAt(word.length - 1);

const word = "distant";

console.log(word.charAt(word.length - 1));

Additional tip: const middleWord = word.slice(1, -1); will copy from the second to second to last characters of the word into a new string.

Other than this you'll need to handle the edge cases for words that are less than 4 characters long so you don't double up the first/last letter or there just simply aren't enough characters to shuffle around.

const shuffleWord = word => {
  if (word.length < 4) return word;
  const first = word.slice(0, 1);
  const middle = word.slice(1, -1);
  const last = word.slice(-1);
  const shuffled = middle
    .split('')
    .sort(() => 0.5 - Math.random())
    .join('');
  return first + shuffled + last;
};

console.log("this is the result", shuffleWord(""));     // ''
console.log("this is the result", shuffleWord("d"));    // 'd'
console.log("this is the result", shuffleWord("di"));   // 'di'
console.log("this is the result", shuffleWord("dis"));  // 'dis'
console.log("this is the result", shuffleWord("dist")); // scrambled
console.log("this is the result", shuffleWord("dista"));
console.log("this is the result", shuffleWord("distan"));
console.log("this is the result", shuffleWord("distant"));

Put it together:

const str = "i love learning code";

const shuffleWord = (word) => {
  if (word.length < 4) return word;
  const first = word.slice(0, 1);
  const middle = word.slice(1, -1);
  const last = word.slice(-1);
  const shuffled = middle
    .split("")
    .sort(() => 0.5 - Math.random())
    .join("");
  return first + shuffled + last;
};

const processSentence = (sentence) =>
  sentence
    .split(/("[^"]+"|[^"\s]+)/g)
    .map(shuffleWord)
    .join("");
    
console.log(processSentence(str));
Drew Reese
  • 165,259
  • 14
  • 153
  • 181