如何在我的代码中使用For循环?

时间:2014-09-17 00:32:00

标签: javascript for-loop

var string1 = "I am going to school.";
var string2 = "We need five different strings.";
var string3 = "Ace has to use the bathroom.";
var string4 = "Alex cant think of anything thing.";
var string5 = "Fingers are sometimes called digits.";

var string1word =(string1.substring(0,string1.search(" ")));

var string2word =(string2.substring(0,string2.search(" ")));

var string3word =(string3.substring(0,string3.search(" ")));

var string4word =(string4.substring(0,string4.search(" ")));

var string5word =(string5.substring(0,string5.search(" ")));


confirm(string1word+", " +", "+ string2word +", "+ string3word +", "+ string4word +", "+ string5word);

你好,我的任务是创建一个"程序"它会输出任何字符串的第一个单词。我完成了这项任务,但它非常重复。我的老师告诉我使用for循环功能来清理它,但我不知道如何在这里格式化或使用for循环。我想知道是否有人可以帮助我使用for循环并解释它为什么/如何在这里工作。

2 个答案:

答案 0 :(得分:1)

免责声明:我在StackOverflow上接触家庭作业问题的唯一原因是因为您似乎已经很好地尝试完成了自己的作业。您的代码按原样运行,但您正在寻找改进代码以满足其他条件的方法。这是值得尊敬的职业道德,我很乐意在这种情况下帮助你。

// create a reusable function
function firstWord(str) {
  return str.substring(0, str.search(" "));
}

// define strings
var string1 = "I am going to school.";
var string2 = "We need five different strings.";
var string3 = "Ace has to use the bathroom.";
var string4 = "Alex cant think of anything thing.";
var string5 = "Fingers are sometimes called digits.";

// put strings in an array
var strings = [string1, string2, string3, string4, string5];

// use a loop to iterate through the array
for (var i = 0, len = strings.length; i < len; i++) {

  // alert the firstWord of each string
  alert(firstWord(strings[i]));
}

输出

I
We
Ace
Alex
Fingers

您还可以跳过单独分配string1string2,... stringN并直接将它们放入strings数组

// define strings
var strings = [
  "I am going to school.",
  "We need five different strings.",
  "Ace has to use the bathroom.",
  "Alex cant think of anything thing.",
  "Fingers are sometimes called digits."
];

// for loop here ...

编辑:如果您需要在代码中使用一个长字符串中的输出,则可以执行以下操作。

// create a reusable function
function firstWord(str) {
  return str.substring(0, str.search(" "));
}

// define strings
var strings = [
  "I am going to school.",
  "We need five different strings.",
  "Ace has to use the bathroom.",
  "Alex cant think of anything thing.",
  "Fingers are sometimes called digits."
];

// set savedWords array
var savedWords = [];

// use a loop to iterate through the array
for (var i = 0, len = strings.length; i < len; i++) {

  // store first word in savedWords array
  savedWords[i] = firstWord(strings[i]);
}

// output list of saved words separated by a comma
confirm(savedWords.join(", "));

这里的大部分代码都是相同的,但我们不是在每个单词上调用alert,而是将第一个单词保存到新的savedWords数组中。

在我们找到每个字符串的第一个单词后,我们在savedWords上调用.join,将单词列表输出为单个字符串。


高级:有更方便的方法,但您的老师可能不允许使用更高级别的功能。这是我能编写的符合程序标准的最短程序

// create a reusable function
function firstWord(str) {
  return str.substring(0, str.search(" "));
}

// define strings
var strings = [
  "I am going to school.",
  "We need five different strings.",
  "Ace has to use the bathroom.",
  "Alex cant think of anything thing.",
  "Fingers are sometimes called digits."
];

// output firstWord-mapped array of strings
confirm(strings.map(firstWord).join(", "));

这使用了.map,这对于初学者来说有点先进。基本思路是.map允许您为数组中的每个项调用一个函数,并返回一个包含每个函数调用结果的新数组。

这是另一个可能有助于演示.map工作原理的简单示例

// square a number function
function squareNumber(n) {
  return n * n;
}

// set some numbers
var numbers = [1, 2, 3];

// get an array of the squares
var squares = numbers.map(squareNumber);

// output
confirm(squares.join(", "));

输出

1, 4, 9

作为单个表达式,对于那些说“#34;代码可能更短&#34;”的反对者,您甚至可以将整个程序编写为单个表达式

confirm([
  "I am going to school.",
  "We need five different strings.",
  "Ace has to use the bathroom.",
  "Alex cant think of anything thing.",
  "Fingers are sometimes called digits."
].map(function(s) { return s.substring(0, s.search(" ")); }).join(", "));

//=> I, We, Ace, Alex, Fingers

然而,你牺牲了很多可读性。我只是向您展示这个示例,以证明JavaScript非常具有表现力,并允许您以非常简洁的方式将事物链接在一起。

无论如何,这里的教训是不要以这种方式编写代码只是因为你可以。更短的代码并不总是意味着更好。

答案 1 :(得分:0)

请试试这个

var s = [
  "I am going to school.",
  "We need five different strings.",
  "Ace has to use the bathroom.",
  "Alex cant think of anything thing.",
  "Fingers are sometimes called digits."
];

for (var i=0, firstWord; i<s.length; i++){
  firstword = s[i].substring(0,s[i].search(" "));
  confirm(firstword);
}