Search code examples
javascriptjqueryarrayssortingmessage

Showing message of the week's day


I am trying to implement a small script, to display in order the messages form an array, for every week's day is another array. I started it but I got stuck. The code is

let daySun = ['msg1','msg2','msg3','msg4'];
let dayMon = ['msg1','msg2','msg3','msg4'];
let dayTue = ['msg1','msg2','msg3','msg4'];
let dayWed = ['msg1','msg2','msg3','msg4'];
let dayThu = ['msg1','msg2','msg3','msg4'];
let dayFri = ['msg1','msg2','msg3','msg4'];
let daySat = ['msg1','msg2','msg3','msg4'];

let dayWeek = new Date().getDay();
//How to get

setInterval(function(){
// 1. to compare name of the day with above arrays
// 2. if today is Sun, then messages from daySun array to be displayed in  order
// 3. and so on for every day of the week
// 4. I want to add fadeIn or fadeOut, or a animate class from  animate.min.css

},1000);

//html for messages is <p id="day-msg"></p>

Solution

  • Done refer js fiidle

    html

    <head>
      <link rel="stylesheet"
      href="https://cdn.jsdelivr.net/npm/[email protected]/animate.min.css">
      <!-- or -->
    </head>
    
    
    <h1 class="animated infinite fadeOut" id="day-msg"></h1>
    

    java script

    let day ={"sun":['msg1','msg2','msg3','msg4'],"mon":['msg1','msg2','msg3','msg4'],"Tue":['msg1','msg2','msg3','msg4'],"wed":['msg1','msg2','msg3','msg4'],"thu":['msg1','msg2','msg3','msg4'],"fri":['msg1','msg2','msg3','msg4'],"sat":['msg1','msg2','msg3','msg4']};
    
    
    let dayName = new Date().toString().split(' ')[0].toLowerCase()
    
    currentIndex = 0;
    setInterval(function(){
    
    document.getElementById("day-msg").innerHTML = day[dayName][currentIndex++];
     if(currentIndex >= day[dayName].length){
       currentIndex= 0;
      } 
    },1000);