首页 > 解决方案 > display next weekday only after asking

问题描述

I have some problem understanding how to resolve this. Loop the output of days of the week like this: "Day of the week. Do you want to see the next day?" And so on as long as the user clicks OK.

var a = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
var again = false;
do {
  var question = prompt("Do you want to see the next day? (Yes / No)");
  if (question == 'Yes') {
    array();
    var question;
    again = true;
    break;
  }
} while (!again);

function array() {
  for (i = 0; i < a.length; i++) {
    console.log(a[i]);
  }
}

标签: javascriptarraysloopswhile-loopdo-while

解决方案


You should check the real console to see the output step by step.enter image description here

Stackoverflow does not output step by step.

var idx = 0;
var a = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
var again = false;
while (true) {
  var question = prompt("Do you want to see the next day? (Yes / No)");
  again = (question == 'Yes');
  if (again) {
    idx++;
    console.log(a[idx]);
  } else {
    break;
  }
  
}


推荐阅读