-
Notifications
You must be signed in to change notification settings - Fork 0
/
dogwalking.js
125 lines (115 loc) · 2.94 KB
/
dogwalking.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* This is a JavaScript Scratchpad.
*
* Enter some JavaScript, then Right Click or choose from the Execute Menu:
* 1. Run to evaluate the selected text (Ctrl+R),
* 2. Inspect to bring up an Object Inspector on the result (Ctrl+I), or,
* 3. Display to insert the result in a comment after the selection. (Ctrl+L)
*/
var diary = [
{
day: 'Mon',
bookings: {
'9am': '',
'10am': 'Bowser',
'11am': '',
'12pm': '',
'1pm': '',
'2pm': 'whatsupyo'
}
},
{
day: 'Tue',
bookings: {
'9am': '',
'10am': '',
'11am': '',
'12pm': '',
'1pm': 'Fifi',
'2pm': 'bruno'
}
}
];
var timesOfDay = [
'9am',
'10am',
'11am',
'12pm',
'1pm',
'2pm'
];
var max = function (num1, num2) {
if (num1 < num2) return num2;
else return num1;
}
var printSchedule = function (diary) {
var entireString = '';
var guardband = 3;
//padding function(item, spaces)
// return padded string;
function padString(eachString, totalSpaces) {
if (eachString.length < totalSpaces) {
var gap = totalSpaces - eachString.length;
return eachString + Array(gap + 1).join(' ');
}
else return eachString;
}
//get required spaces for 1st column (9am, 10am)
// for timesOfDay
// max Length + guardband;
firstColumnSize = timesOfDay.map(function (eachItem) {
return eachItem.length;
}).reduce(max
) + guardband;
//get required spaces for Mon, Name columns
// for diary.day
// max Length + guardband;
// for bookings
// max name_length + guardband;
otherColumnSize = max(
diary.map(function (eachEntry) {
return eachEntry.day.length;
}).reduce(max
),
diary.map(function (eachEntry) {
var eachBookings = eachEntry.bookings;
var bookingNames = timesOfDay.map(function (eachTime) {
return eachBookings[eachTime].length;
})
return bookingNames.reduce(max
);
}).reduce(max
)
) + guardband;
//print header
// blank, mon, tue, wed, thurs
var headerString = '';
headerString += padString('', firstColumnSize);
diary.forEach(function (eachEntry) {
headerString += padString(eachEntry.day, otherColumnSize);
});
console.log(headerString);
entireString += headerString;
//print row for each timesOfDay
// time, Bowser, fifi, "", ""
timesOfDay.forEach(function (eachTimeSlot) {
var otherString = '';
otherString += padString(eachTimeSlot, firstColumnSize);
diary.forEach(function (eachEntry) {
otherString += padString(eachEntry.bookings[eachTimeSlot], otherColumnSize);
});
console.log(otherString);
entireString+= "\n" + otherString;
});
return entireString;
}
printSchedule(diary);
/*
Mon Tue
9am
10am Bowser
11am
12pm
1pm Fifi
2pm whatsupyo bruno
*/