Question
Code to start with: var recentLogins = { Elvis Presley: new Date(2020, 0, 8), Sonya Sotomayor: new Date(2020, 1, 25), Franklin D. Roosevelt: new Date(2020,
Code to start with:
var recentLogins = { "Elvis Presley": new Date(2020, 0, 8), "Sonya Sotomayor": new Date(2020, 1, 25), "Franklin D. Roosevelt": new Date(2020, 0, 30), "Ben Carson": new Date(2020, 1, 18), "Roger Staubach": new Date(2020, 1, 5), "Steve Jobs": new Date(2020, 1, 24), "Albert Einstein": new Date(2020, 1, 14), "Isaac Asimov": new Date(2020, 0, 4), "Jada Pinkett Smith": new Date(2020, 1, 18), "Grace Hopper": new Date(2020, 0, 9) };
// Display all names and logins for (var person in recentLogins) { console.log(person + ": " + recentLogins[person]); }
// Find who is closest in login time to Sonya Sotomayor var sonyaLogin = recentLogins["Sonya Sotomayor"].getTime(); var timeDifference = Infinity; for (person in recentLogins) { // See if this person's login difference is closer if (person != "Sonya Sotomayor" && Math.abs(recentLogins[person].getTime() - sonyaLogin) < timeDifference) { timeDifference = Math.abs(recentLogins[person].getTime() - sonyaLogin); } }
console.log(" Closest Login difference is " + timeDifference);
// Find who logged in on the same day of the week console.log(" Logins on same day of the week:"); var names = Object.keys(recentLogins); var i, j; for (i = 0; i < names.length - 1; i++) { for (j = i + 1; j < names.length; j++) { console.log("Comparing " + names[i] + " to " + names[j]); console.log(recentLogins[names[i]].getDay() + " and " + recentLogins[names[j]].getDay()); } }
// Convert the day number (0-6) into a day name (Sunday-Saturday) function convertDayNumberToWord(dayNum) { var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; return days[dayNum]; }
The recentLogins associative array contains a list of some employees and their remote VPN login dates. Ex: recentLogins["Elivs Presley"] contains Elvis' login date, which is Jan 8, 2020.
-
The first for-in loop displays each employees's name and login date, but the format of the date is too wordy. Change the output format to MM/DD/YYYY. Ex: Elvis Presley: 0/8/2020.
-
The second for-in loop determines which of the login date is closest to Sonya Sotomayor's login. The difference in milliseconds of the closest login is output to the console. Change the output to days by dividing the number of milliseconds by the number of milliseconds in a day. Hint: 1000 milliseconds are in a second, 60 seconds are in a minute, etc.
-
Display the employee who has the login closest to Sonya Sotomayor's. Add a variable that keeps track of which name is processed when timeDifference is assigned. Use the variable to output the name when outputting the number of days.
-
The final set of nested for loops outputs a "Comparing" message to the console with two names and the day of the week (as a number) for the two individuals' logins. Modify the code to output only those individuals whose logins are on the same day of the week.
Ex: "Elvis Presley and Roger Staubach on Wednesday".
The Date object does not have a way to convert a day number like 2 to the string "Tuesday", so use the convertDayNumberToWord() function that is provided.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started