Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* eslint-disable no-multiple-empty-lines */
- function hasNewMessage() {
- // TODO: return true with a probability of 20%.
- randNum = Math.random();
- console.log(randNum);
- return randNum < 0.2;
- }
- function newMessage() {
- // TODO: return a random message as an object with two keys, subject and sender
- const subjects = ["your late", "nice job", "OMG your on time"];
- const senders = ["Shannon", "Carlos", "Maria"];
- let subjectRandom = subjects[Math.floor(Math.random() * 3)];
- let senderRandom = senders[Math.floor(Math.random() * 3)];
- let message = {
- subject: subjectRandom,
- sender: senderRandom
- };
- return message;
- }
- function appendMessageToDom(message) {
- // TODO: append the given message to the DOM (as a new row of `#inbox`)
- const inbox = document.getElementById("inbox");
- inbox.insertAdjacentHTML("afterbegin", `<div class="row message unread"><div class="col-xs-3">${message.sender}</div><div class="col-xs-9">${message.subject}</div></div>`);
- // Get the element #count
- const counter = document.getElementById("count");
- // Get all unread messages => array
- const unreadMessages = document.querySelectorAll('.unread');
- counter.innerText = `(${unreadMessages.length})`;
- }
- function refresh() {
- // TODO: Implement the global refresh logic. If there is a new message,
- // append it to the DOM. Update the unread counter in title as well.
- if (hasNewMessage()) {
- appendMessageToDom(newMessage());
- }
- }
- // Do not remove these lines:
- document.addEventListener("DOMContentLoaded", () => {
- setInterval(refresh, 1000); // Every 1 second, the `refresh` function is called.
- });
- module.exports = { hasNewMessage, newMessage };
Add Comment
Please, Sign In to add comment