break 和 continue 语句
·
Yin灏
break 用来终止循环;continue 终止当前循环,开启下一次循环。
let weekday = ['Monday', 'Tuesday', 'Wednessday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
let message = 'The working days of this week: ';
let bnum = 3 + parseInt(4 * Math.random());
let cnum01 = parseInt(3 * Math.random());
let cnum02 = 3 + parseInt(4 * Math.random());
for (let i = 0; i < weekday.length; i++) {
if (i == bnum) break;
message += weekday[i] + ', ';
}
message = message.slice(0, -2);
console.log(message);
console.log('');
message = 'Cannot have day-off on: ';
for (let i = 0; i < weekday.length; i++) {
if (i == cnum01 || i == cnum02) continue;
message += weekday[i] + ', ';
}
message = message.slice(0, -2);
console.log(message);