Appendix B: Practice, Practice, Practice! - hochan222/Everything-in-JavaScript GitHub Wiki

Practicing Comparisons

const dayStart = "07:30"; 
const dayEnd = "17:45";

function scheduleMeeting(startTime,durationMinutes) { 
  // ..TODO..
}
scheduleMeeting("7:00",15);   // false
scheduleMeeting("07:15",30);  // false
scheduleMeeting("7:30",30);   // true
scheduleMeeting("11:30",60);  // true
scheduleMeeting("17:00",45);  // true
scheduleMeeting("17:30",30);  // false
scheduleMeeting("18:00",15);  // false
📄 답지
const dayStart = "07:30";
const dayEnd = "17:45";

function scheduleMeeting(startTime, durationMinutes) {
    var startTimeMinute = timeToMinute(startTime);

    console.log(timeToMinute(dayStart) <= startTimeMinute
            && startTimeMinute + durationMinutes <= timeToMinute(dayEnd));
    return ;

    function timeToMinute (time) {
        var [hour, minute] = time.split(':');
        return (Number(minute) + Number(hour) * 60);
    }
}

scheduleMeeting("7:00", 15);
scheduleMeeting("07:15", 30);
scheduleMeeting("7:30", 30);
scheduleMeeting("11:30", 60);
scheduleMeeting("17:00", 45);
scheduleMeeting("17:30", 30);
scheduleMeeting("18:00", 15);

Practicing Closure

function range(start,end) { 
  // ..TODO..
}

range(3,3);  // [3]
range(3,8);  // [3,4,5,6,7,8]
range(3,0);  // []

var start3 = range(3); 
var start4 = range(4);

start3(3);  // [3]
start3(8);  // [3,4,5,6,7,8]
start3(0);  // []
start4(6);  // [4,5,6]
📄 답지
function range(start,end) { 
  start = Number(start) || 0;

  if (end === undefined) {
    return function getEnd(end) {
      return getRange(start,end); 
    };
  } else {
    end = Number(end) || 0;
    return getRange(start,end); 
  }
 
  function getRange(start,end) { 
    var ret = [];

    for (let i = start; i <= end; i++) { 
      ret.push(i);
    }
    return ret; 
  }
}

Practicing Prototypes

📄 답지

⚠️ **GitHub.com Fallback** ⚠️