I have an input like this: var names = ["Ram","Shyam","Neel","Nik"];
I need to display these names in a table which will be tagged against Weekend, for Ex:
Points: -This table will keep generating the names for weekends month by month. Here is the tried code but it is not generating any result. Not sure what mistake i have made.
I am quite new to JS and tried below code, Need some help here: https://www.jdoodle.com/h/1IW
Here is the Code snippet:
const is_weekend = function(dt){
if(dt.getDay() == 6 || dt.getDay() == 0) return true;
else false;
}
var array = ["Tom","Taylor","Nick","John"];
var currArrIndex = 0;
var start = new Date(); //current date
var end = new Date(start.getFullYear(), start.getMonth()+1, 0); //last date of the month
var rows = Array();
var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
var currDate = start;
console.log(currDate);
while(currDate <= end){
//alert(currDate);
console.log(currDate);
if(is_weekend(currDate)){ //if the date is a weekend date
let dayName = days[currDate.getDay()]; //get week day of the current date
let rowElement = {
name : array[currArrIndex], //set one of the names from array from first to last
day : dayName,
date : currDate.toLocaleDateString()
}
rows.push(rowElement);
if(currDate.getDay() == 0){ //change after every week
currArrIndex ++; //current index array name is already assigned to last weekend, hence move to next array name
currArrIndex %= array.length; //if current index goes beyond array length resign to 0
}
}
var newDate = currDate.setDate(currDate.getDate() + 1);
currDate = new Date(newDate);
}
var html = "<table border='1|1'>";
for (var i = 0; i < rows.length; i++) {
let htmlElement = `
<tr>
<td>${rows[i].name}</td>
<td>${rows[i].day}</td>
<td>${rows[i].date}</td>
</tr>`;
html+=htmlElement;
}
html+="</table>";
document.getElementById("box").innerHTML = html;