【发布时间】:2017-01-25 21:16:00
【问题描述】:
我在尝试完成我的代码时遇到问题。我试图让代码在几个不同的行中打印数组。应该是这样的:
好的,这是更新的代码。我一直在努力,它正在变得更好!但是,我现在的问题是试图弄清楚我需要做什么才能创建代码输出中当前存在“0”和“1”的空格。我想我只需要插入: calDaysOfWeek.unshift(); 或者可能: calDaysOfWeek.shift(); 我只是不知道把它放在哪里。如果我能弄清楚这一点,我应该能够减少代码中的日期缩写(例如 M 到 Mon 或 T 到 Tues)。
/*Write a program called calendar.js that displays a calendar month
for May 2012 as the month and year. You must use a loop. The format
of the month should be as shown below: Hint: You can't use console
log and print on the same line. Try storing the entire row as one
concatenated string and then displaying it. You must use loops for
this one. Do not just print the calendar as a series of console.log
statements!*/
calDaysOfWeek = [];
calDaysOfWeek.push("Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat");
var s = "MAY 2012\nS M T W T F S\n";
var numDaysOfWeek = calDaysOfWeek.length;
var firstDay = 2;
var numDaysOfMonth = 31;
var numWeeks = numDaysOfMonth/7;
for(var i = 0; i < numWeeks; i++) {
for(var j = 0; j < numDaysOfWeek; j++) {
s += i*numDaysOfWeek + j + ' ';
}
s += "\n";
}
if (firstDay != calDaysOfWeek[2]) {
calDaysOfWeek.push();
}
else if (calDaysOfWeek[30] != 31) {
calDaysOfWeek.pop();
}
else
console.log(s);
【问题讨论】:
标签: javascript arrays loops while-loop nested-loops