Suggested Solutions

Keep in mind that these suggested solutions are just that: suggestions. There’s many different ways to solve these practice exercises. Compare your approach to what you see here, and consider the pros and cons of each.

Suggested solution for “Comparisons” (Pillar 3) practice:

  1. const dayStart = "07:30";
  2. const dayEnd = "17:45";
  3. function scheduleMeeting(startTime,durationMinutes) {
  4. var [ , meetingStartHour, meetingStartMinutes ] =
  5. startTime.match(/^(\d{1,2}):(\d{2})$/) || [];
  6. durationMinutes = Number(durationMinutes);
  7. if (
  8. typeof meetingStartHour == "string" &&
  9. typeof meetingStartMinutes == "string"
  10. ) {
  11. let durationHours =
  12. Math.floor(durationMinutes / 60);
  13. durationMinutes =
  14. durationMinutes - (durationHours * 60);
  15. let meetingEndHour =
  16. Number(meetingStartHour) + durationHours;
  17. let meetingEndMinutes =
  18. Number(meetingStartMinutes) +
  19. durationMinutes;
  20. if (meetingEndMinutes >= 60) {
  21. meetingEndHour = meetingEndHour + 1;
  22. meetingEndMinutes =
  23. meetingEndMinutes - 60;
  24. }
  25. // re-compose fully-qualified time strings
  26. // (to make comparison easier)
  27. let meetingStart = `${
  28. meetingStartHour.padStart(2,"0")
  29. }:${
  30. meetingStartMinutes.padStart(2,"0")
  31. }`;
  32. let meetingEnd = `${
  33. String(meetingEndHour).padStart(2,"0")
  34. }:${
  35. String(meetingEndMinutes).padStart(2,"0")
  36. }`;
  37. // NOTE: since expressions are all strings,
  38. // comparisons here are alphabetic, but it's
  39. // safe here since they're fully qualified
  40. // time strings (ie, "07:15" < "07:30")
  41. return (
  42. meetingStart >= dayStart &&
  43. meetingEnd <= dayEnd
  44. );
  45. }
  46. return false;
  47. }
  48. scheduleMeeting("7:00",15); // false
  49. scheduleMeeting("07:15",30); // false
  50. scheduleMeeting("7:30",30); // true
  51. scheduleMeeting("11:30",60); // true
  52. scheduleMeeting("17:00",45); // true
  53. scheduleMeeting("17:30",30); // false
  54. scheduleMeeting("18:00",15); // false

Suggested solution for “Closure” (Pillar 1) practice:

  1. function range(start,end) {
  2. start = Number(start) || 0;
  3. if (end === undefined) {
  4. return function getEnd(end) {
  5. return getRange(start,end);
  6. };
  7. }
  8. else {
  9. end = Number(end) || 0;
  10. return getRange(start,end);
  11. }
  12. // **********************
  13. function getRange(start,end) {
  14. var ret = [];
  15. for (let i = start; i <= end; i++) {
  16. ret.push(i);
  17. }
  18. return ret;
  19. }
  20. }
  21. range(3,3); // [3]
  22. range(3,8); // [3,4,5,6,7,8]
  23. range(3,0); // []
  24. var start3 = range(3);
  25. var start4 = range(4);
  26. start3(3); // [3]
  27. start3(8); // [3,4,5,6,7,8]
  28. start3(0); // []
  29. start4(6); // [4,5,6]

Suggested solution for “Prototypes” (Pillar 2) practice:

  1. function randMax(max) {
  2. return Math.trunc(1E9 * Math.random()) % max;
  3. }
  4. var reel = {
  5. symbols: [
  6. "♠", "♥", "♦", "♣", "☺", "★", "☾", "☀"
  7. ],
  8. spin() {
  9. if (this.position == null) {
  10. this.position = randMax(
  11. this.symbols.length - 1
  12. );
  13. }
  14. this.position = (
  15. this.position + 100 + randMax(100)
  16. ) % this.symbols.length;
  17. },
  18. display() {
  19. if (this.position == null) {
  20. this.position = randMax(
  21. this.symbols.length - 1
  22. );
  23. }
  24. return this.symbols[this.position];
  25. }
  26. };
  27. var slotMachine = {
  28. reels: [
  29. Object.create(reel),
  30. Object.create(reel),
  31. Object.create(reel)
  32. ],
  33. spin() {
  34. this.reels.forEach(function spinReel(reel){
  35. reel.spin();
  36. });
  37. },
  38. display() {
  39. var lines = [];
  40. // display all 3 lines on the slot machine
  41. for (
  42. let linePos = -1; linePos <= 1; linePos++
  43. ) {
  44. let line = this.reels.map(
  45. function getSlot(reel){
  46. var slot = Object.create(reel);
  47. slot.position = (
  48. reel.symbols.length +
  49. reel.position +
  50. linePos
  51. ) % reel.symbols.length;
  52. return slot.display();
  53. }
  54. );
  55. lines.push(line.join(" | "));
  56. }
  57. return lines.join("\n");
  58. }
  59. };
  60. slotMachine.spin();
  61. slotMachine.display();
  62. // ☾ | ☀ | ★
  63. // ☀ | ♠ | ☾
  64. // ♠ | ♥ | ☀
  65. slotMachine.spin();
  66. slotMachine.display();
  67. // ♦ | ♠ | ♣
  68. // ♣ | ♥ | ☺
  69. // ☺ | ♦ | ★

That’s it for this book. But now it’s time to look for real projects to practice these ideas on. Just keep coding, because that’s the best way to learn!