Guest User

Untitled

a guest
Feb 25th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. /* eslint-disable no-multiple-empty-lines */
  2.  
  3. function hasNewMessage() {
  4. // TODO: return true with a probability of 20%.
  5. randNum = Math.random();
  6. console.log(randNum);
  7. return randNum < 0.2;
  8. }
  9.  
  10. function newMessage() {
  11. // TODO: return a random message as an object with two keys, subject and sender
  12. const subjects = ["your late", "nice job", "OMG your on time"];
  13. const senders = ["Shannon", "Carlos", "Maria"];
  14. let subjectRandom = subjects[Math.floor(Math.random() * 3)];
  15. let senderRandom = senders[Math.floor(Math.random() * 3)];
  16. let message = {
  17. subject: subjectRandom,
  18. sender: senderRandom
  19. };
  20. return message;
  21. }
  22.  
  23. function appendMessageToDom(message) {
  24. // TODO: append the given message to the DOM (as a new row of `#inbox`)
  25. const inbox = document.getElementById("inbox");
  26. 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>`);
  27.  
  28. // Get the element #count
  29. const counter = document.getElementById("count");
  30.  
  31. // Get all unread messages => array
  32. const unreadMessages = document.querySelectorAll('.unread');
  33.  
  34.  
  35. counter.innerText = `(${unreadMessages.length})`;
  36.  
  37. }
  38.  
  39. function refresh() {
  40. // TODO: Implement the global refresh logic. If there is a new message,
  41. // append it to the DOM. Update the unread counter in title as well.
  42. if (hasNewMessage()) {
  43. appendMessageToDom(newMessage());
  44. }
  45.  
  46.  
  47. }
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. // Do not remove these lines:
  66. document.addEventListener("DOMContentLoaded", () => {
  67. setInterval(refresh, 1000); // Every 1 second, the `refresh` function is called.
  68. });
  69.  
  70. module.exports = { hasNewMessage, newMessage };
Add Comment
Please, Sign In to add comment