Edabit: Jake's Meal Time
// Jake's Meal Time
// Jake eats breakfast at 7am, lunch at 12pm, and dinner at 7pm.
// Using the current time, calculate hours and minutes until Jake's next meal, and specify which meal that is.
// We act like current time's seconds is always 0. We'll calculate to the hour before and then add in the minutes left (if minutes left would be 60, we change minutes left to 0 and add one more 'hour until')
function mealTicket(time){
// input
var date = new Date(time);
var hour = date.getHours();
var min = date.getMinutes();
// output
var mealName = "N/A";
var hoursTil = 0;
var minsTil = 0;
// logic
if(hour < 7 || hour >= 19){ // calc for breaky-boo
mealName = "breakfast";
if(hour >= 19){
hoursTil = 24-hour;
hour = 0;
}
hoursTil += 6 - hour;
if(min == 0) hoursTil += 1;
else minsTil = 60 - min;
}else if(hour < 12){ // calc for lunch
mealName = "lunch";
hoursTil += 11 - hour;
if(min == 0) hoursTil += 1;
else minsTil = 60 - min;
}else if(hour < 19){
mealName = "dinner";
hoursTil += 18 - hour;
if(min == 0) hoursTil += 1;
else minsTil = 60 - min;
}
console.log("Jake's next meal is " + mealName + " in " + hoursTil + " hours, " + minsTil + " minutes. His next meal ticket is for the time: " + date);
}
function test(){ // generate random date
var n = Math.floor(Math.random() * 4) + 1;
var op = Math.floor(Math.random() * 4);
var nowDate = (new Date()).valueOf();
if(op == 0) mealTicket(nowDate + (n * 1000000));
else if(op == 1) mealTicket(nowDate - (n * 1000000));
else if(op == 2) mealTicket(nowDate * n);
else if(op == 3) mealTicket(nowDate / n);
else mealTicket(nowDate);
}
mealTicket((new Date()).valueOf());
test();
test();
test();
test();
test();
No comments:
Post a Comment