结合数组,循环和if语句

时间:2017-05-18 14:48:26

标签: javascript arrays for-loop if-statement

我试图了解如何应用数组,循环和if语句。我已经创建了一个工作日数组,我想指定在特定日期应该分配某些练习。我还要打印出分配的日期和练习。正如您将从下面的代码中看到的那样,使用weekday.push()方法在隔天分配游泳或举重训练,根本不添加瑜伽并创建14个值。你能否建议如何解决这个问题。谢谢!



var weekday = ['Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat'];
var workOut = [];

function myExercise() {
  for (var i = 0; i < weekday.length; i++) {
    if (weekday == 'Wed' || 'Mon' || 'Fri') {
      workOut.push('Swimming');
    }
    if (weekday == 'Tue' || 'Thur' || 'Sat') {
      workOut.push('weight training');
    } else {
      workOut.push('Yoga' + 'or' + 'power walking');
    }
  }
}

myExercise();
console.log(workOut);
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:4)

不幸的是,您不能像在if子句中尝试的那样使用捷径。你也忘记了迭代器[i]。除了正常

if (weekday[i] == 'Wed' || weekday[i] == 'Mon' ||  weekday[i] == 'Fri')

你也可以使用以下表格:

if ( ['Wed','Mon','Fri'].includes(weekday[i]))

如果您想支持IE(不支持includes),您可以使用indexOf

每当你想检查很多值时,这都很方便。总而言之,你可以这样写:

注意,我还将您的第二个if更改为else if(这是14个条目的原因)并使用了Array.prototype.map功能。此外,操纵不属于您的范围的对象是一个坏习惯(锻炼在全局范围内,但由myExercise函数操纵)。最好返回所需的结果,然后进行简单的分配。

var weekday = ['Sun', 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat'];
var workOut = [];

function myExercise() {

  return weekday.map(function(day){
      var workout;
      if (['Mon','Wed','Fri'].includes(day)){
        workout = "Swimming";
      } else if (['Tue','Thur','Sat'].includes(day)){
        workout = "Weight training";
      } else {
        workout = 'Yoga or power walking';
      }
      return workout;
  });
}

workOut = myExercise();
console.log(workOut);

答案 1 :(得分:1)

修改了你的代码,拼写错误和条件重叠

&#13;
&#13;
var weekday = ['Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat'];
var workOut = [];

function myExercise() {
  for (var i = 0; i < weekday.length; i++) {
  console.log(weekday[i] )
    if (weekday[i] == 'Wed' || weekday[i] == 'Mon' || weekday[i] == 'Fri') {
      workOut.push('Swimming');
    }
    else{
    if (weekday[i] == 'Tues' || weekday[i] == 'Thur' || weekday[i] == 'Sat') {
      workOut.push('weight training');
    } else {
      workOut.push('Yoga' + 'or' + 'power walking');
    }
    }
  }
}

myExercise();
console.log(workOut);
VM280:18 
&#13;
&#13;
&#13;

答案 2 :(得分:1)

&#13;
&#13;
var weekdays = ['Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat'];
var workouts = weekdays.map(day => {
  switch (day) {
    case 'Wed':
    case 'Mon':
    case 'Fri':
      return 'Swimming';
      break;
    case 'Tues':
    case 'Thur':
    case 'Sat':
      return 'weight training';
      break;
    default:
      return 'Yoga or power walking';
      break;
  }
});

console.log(workouts);
&#13;
&#13;
&#13;

答案 3 :(得分:-1)

上一个答案中已有switch语句,但我也包含了这个。它可能不是DRY,但对于初学者来说更容易理解:

var weekday = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
var workOut = [];

function myExercise() {    
    for (var i = 0; i < weekday.length; i++) {
        switch(weekday[i]) {
            case 'Sun':
                workOut.push('Yoga' + 'or' + 'power walking');
                break;
            case 'Mon':
                workOut.push('Swimming');
                break;
            case 'Tue':
                workOut.push('weight training');
                break;
            case 'Wed':
                workOut.push('Swimming');
                break;
            case 'Thu':
                workOut.push('weight training');
                break;
            case 'Fri':
                workOut.push('Swimming');
                break;
            case 'Sat':
                workOut.push('weight training');
                break;
                default:
                workOut.push('Sleep!!');
        }
    }
}

myExercise();
console.log(workOut);