嘗試打亂句子中的單詞并將它們重新組合成一個陣列。洗牌不是什么大問題,但重新組合是問題所在。
(我只能在第一個字母之后打亂所有內容,因此為什么事情要復雜一些。)
當前輸出為 ["I"] ["leik"] ["bnoca"]
期望的輸出 ["I", "leik", "bnoca"]
嘗試創建一個名為“empty”的空陣列。然后我想將專案推入陣列中,但是由于 for 回圈而出現問題?我想?
這是影像中的代碼筆。由于某種原因無法在此處上傳代碼?https://codepen.io/halfrussian/pen/gOoRKMj
const scrambleWord = () => {
let thisString = "I like bacon";
letOfficialSentence = thisString.split(" ")
for(let i = 0; i < letOfficialSentence.length; i ){
let thisFirstWord = letOfficialSentence[i];
let thisFirstLetter = letOfficialSentence[i][0];
let thisSubString = thisFirstWord.substring(1)
//shuffler
//see https://stackoverflow.com/questions/3943772/how-do-i-shuffle-the-characters-in-a-string-in-javascript
String.prototype.shuffle = function () {
var a = this.split(""),
n = a.length;
for(var l = n - 1; l > 0; l--) {
var j = Math.floor(Math.random() * (l 1));
var tmp = a[l];
a[l] = a[j];
a[j] = tmp;
}
return a.join("");
}
//end of shuffler
let newerWords = thisFirstLetter thisSubString.shuffle()
let empty = [];
empty.push(newerWords)
console.log(empty)
}
}
uj5u.com熱心網友回復:
您正在創建空陣列并在 for 回圈內分別添加每個單詞。
將 empty 的定義移到 for 回圈之前:
let empty = [];
然后在你的回圈之后放置console.log:
console.log(empty);
uj5u.com熱心網友回復:
您創建empty陣列的方法是正確的。但是,這里有一個范圍問題:使用定義的變數let僅在當前范圍內有效,這意味著由花括號包圍的塊{}。因此,您let empty將在回圈的每次迭代中重新定義for,導致您的第一個單詞使用另一個“桶”來表示變數,而不是第二個單詞等等。
您可以empty改為定義回圈的外部,以在不同的迭代中保持它:
let thisString = "I like bacon";
letOfficialSentence = thisString.split(" ")
let empty = []; //> Define it outside the loop!
//shuffler
//see https://stackoverflow.com/questions/3943772/how-do-i-shuffle-the-characters-in-a-string-in-javascript
String.prototype.shuffle = function() {
var a = this.split(""),
n = a.length;
for (var l = n - 1; l > 0; l--) {
var j = Math.floor(Math.random() * (l 1));
var tmp = a[l];
a[l] = a[j];
a[j] = tmp;
}
return a.join("");
}
//end of shuffler
for (let i = 0; i < letOfficialSentence.length; i ) {
let thisFirstWord = letOfficialSentence[i];
let thisFirstLetter = letOfficialSentence[i][0];
let thisSubString = thisFirstWord.substring(1);
let newerWords = thisFirstLetter thisSubString.shuffle()
empty.push(newerWords)
console.log(empty)
}
同樣,您也可以shuffle在回圈之外定義 r,因為它不依賴于任何與回圈相關的資料,這將使您的回圈更具可讀性。
uj5u.com熱心網友回復:
嘗試這個:
String.prototype.shuffle = function() {
const a = this.split(''),
n = a.length;
for (let l = n - 1; l > 0; --l) {
const j = Math.floor(Math.random() * (l 1));
[a[l], a[j]] = [a[j], a[l]];
}
return a.join('');
}
const scrambleWord = () => {
const thisString = "I like bacon";
const officialSentence = thisString.split(' ');
const needArray = [];
for (let i = 0; i < officialSentence.length; i) {
const thisFirstWord = officialSentence[i];
const thisFirstLetter = thisFirstWord[0];
const thisSubString = thisFirstWord.substring(1);
needArray.push(thisFirstLetter thisSubString.shuffle());
}
return needArray;
}
console.log(scrambleWord());
uj5u.com熱心網友回復:
您可以在代碼中簡化很多內容。以下函式對sentence作為引數傳遞的引數進行打亂:
const scramble = sentence =>
sentence.split(" ").map(w=>{
const a=w.split("");
for (let l = a.length - 1; l > 1; --l) {
const j = 1 Math.floor(Math.random() * l);
[a[l], a[j]] = [a[j], a[l]];
}
return a.join("");
});
["some silly sentence","And now for something completely different"].forEach(s=>console.log(scramble(s)));
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/452795.html
標籤:javascript 数组 for循环 数据结构
下一篇:有沒有辦法讓for回圈更快?
