简体   繁体   English

如何正确警告数组/ for循环中的项目?

[英]How do I alert items from an array/for loop correctly?

I am doing a project with converting a simple sentence to a statement that an 'adolescent' would say. 我正在做一个项目,将一个简单的句子转换成一个“青少年”会说的陈述。 I want to add the phrase 'you know' after each three words in the sentence that the user types in. When I print the converted sentence at the end, it seems as though there is an extra 'you know' for each one of my items (or words) in my array. 我想在用户键入的句子中的每个三个单词之后添加短语“您知道”。当我在最后打印转换后的句子时,似乎每个我的单词都有一个额外的“您知道”我的阵列中的项目(或单词)。 How do I lose all of the extra 'you know''s at the end? 我如何在最后丢失所有多余的“您知道的”? Or how do I prevent that from happening? 或者如何防止这种情况发生? Here is my code: 这是我的代码:

var k = 3;
var phrase = "you know";
var sentence = prompt("Enter a sentence: ");
sentence = sentence.split(" ");

for (word in sentence) {
  sentence.splice(k, 0, phrase);
  k += 4;
}

sentence = sentence.join(" ");

alert(sentence);

for of vs for in 的of与in

When you want to iterate over every item in an iterable (such as an array), you should use for (let x of arr) ... instead of for (let x in arr) . 当您要遍历可迭代对象(例如数组)中的每个项目时,应使用for (let x of arr) ...代替for (let x in arr) The latter is used for iterating over every key in an object, including prototypically inherited ones. 后者用于迭代对象中的每个键,包括原型继承的键。

Mutating what you iterate 改变您迭代的内容

You are iterating over a thing and modifying it at the same time, which is always a good recipe for a headache. 您正在迭代一个事物并同时对其进行修改,这始终是头痛的良方。 If you are going to mutate an array incrementally while traversing it, make a copy first: var newSentence = sentence.slice() , then modify that. 如果要在遍历数组时对其进行增量更改,请首先创建一个副本: var newSentence = sentence.slice() ,然后进行修改。 In fact, you don't even really need to iterate the array because... 实际上,您甚至根本不需要迭代数组,因为...

Too many "you knows" 太多“你知道的”

Your logic is a little bit off. 您的逻辑有些偏离。 For an N-word sentence, you inject "you know" exactly N times; 对于一个N字句子,您要准确地注入N次“您知道”; which only makes sense if you want one "you know" per word. 仅当您希望每个单词一个“您知道”时才有意义。 If you want to insert every 4 words, you need to insert N / 4 times. 如果要每4个字插入一次,则需要插入N / 4次。 What you need is to simply keep inserting while you haven't run off past the end of the sentence: 您需要的是简单地继续插入,直到没有结束句子的结尾:

var k = 3;
var phrase = "you know";
var sentence = prompt("Enter a sentence: ");
sentence = sentence.split(" ");

while (k <= sentence.length) {
  sentence.splice(k, 0, phrase);
  k += 4;
}

sentence = sentence.join(" ");

alert(sentence);

See the for loop below, which starts after the third word and increments by three words (plus the phrase we just inserted) with each iteration, stopping at the end of the sentence: 请参见下面的for循环,该循环在第三个单词之后开始,并在每次迭代时增加三个单词(加上我们刚刚插入的短语),并在句子的结尾处停止:

 var phrase = "you know"; var sentence = "This is a sentence with a bunch of words in it."; // test data instead of prompt var words = sentence.split(" "); for (var i = 3; i <= words.length; i += 4) { words.splice(i, 0, phrase); } console.log(words.join(" ")); // instead of alert 

The issue with your code is that your loop executes n times, where n is the number of words in the sentence. 代码的问题是循环执行n次,其中n是句子中的单词数。 You could fix your loop by break ing when k goes off the end of the sentence, but it's simpler to just construct a better for loop. 您可以通过在k超出句子结尾时break而修复循环,但是构造一个更好的for循环更简单。

Here you go 干得好

var sentence = prompt("Enter a sentence: ");
sentence = sentence.split(" ");
var newSentence = [];
for(var i = 0;i<sentence.length;i++){
 if(i!=0 && i%3==0 && i<(sentence.length-1)){
  newSentence.push("you know");
 }
newSentence.push(sentence[i]);
}
sentence = newSentence.join(" ");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM