【问题标题】:Tricky string scramble in react棘手的字符串争夺反应
【发布时间】:2021-08-11 01:22:12
【问题描述】:

问题需要以特定方式返回一个混洗的字符串:

  • 我们只想“打乱”句子的每个单词,所以单词的顺序保持不变 一样的
  • 每个单词都会保留首尾字母的位置,中间的字母会 炒作。例如,如果原始单词是“animal”,它应该将单词随机化为 类似“aainml”
  • 如果单词只有一两个字符,则保持不变
  • 每句话最多5个词
  • 每个单词最多有 15 个字符
  • 每个单词之间的空格永远不会超过一个。 所以“i love learning code”应该变成“i lvoe lernniag cdoe”。 我如何在 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>
  );
}

这就是我已经走了多远,我的代码唯一的问题是我无法弄清楚如何在末尾插入最后一个字母。

【问题讨论】:

  • SO 不是代码编写服务,请为您的代码添加Minimal, Complete, and Reproducible Code Example,并添加有关任何错误或任何未按预期工作的任何详细信息。似乎一个简单的字符串拆分得到一个单词数组,然后加扰单词就可以了。
  • @mhodges 它实际上是在应用洗牌算法。谷歌使找到一个微不足道的,Fischer-Yates 可能是最佳结果(也是您链接中的最佳答案????)。
  • 谢谢你的回答,我知道怎么打乱,我的代码的问题是结果字符串不包含我单词的最后一个字母。而且我似乎无法找到插入它的方法。

标签: javascript reactjs string scramble


【解决方案1】:

您在尝试抓取最后一个字母时遇到了一个错误。由于索引是 0 索引的,因此您希望 length - 1 访问最后一个字母。

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

const word = "distant";

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

附加提示:const middleWord = word.slice(1, -1); 会将单词的倒数第二个字符复制到一个新字符串中。

除此之外,您还需要处理长度小于 4 个字符的单词的边缘情况,这样您就不会将第一个/最后一个字母加倍,或者根本没有足够的字符来随机播放。

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"));

把它放在一起:

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));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多