Reputation: 1076
Now I'm developing timeline program with javascript. When I click each class, it should be colored to timeline with 2 blocks because each classes are 2hours classes.
each timeline table has id(ex. monday first class has ="mon1" and firday second class has 'fri2'
Here's my code. and It works well. but I want to know how can I reduce the code.
let timelineColor = getRamdomColor();
let clickDay;
let clickDay1;
for (i = 0; i < dayArray.length; i++) {
if (dayArray[i] === 'monday') {
clickDay = 'mon' + timeArray[i];
let table = document.getElementById(clickDay);
table.style.backgroundColor = timelineColor;
clickDay1 = 'mon' + (Number(timeArray[i]) + 1);
console.log(clickDay1);
let table1 = document.getElementById(clickDay1);
table1.style.backgroundColor = timelineColor;
} else if (dayArray[i] === 'tuesday') {
clickDay = 'tue' + timeArray[i];
let table = document.getElementById(clickDay);
table.style.backgroundColor = timelineColor;
clickDay1 = 'tue' + (Number(timeArray[i]) + 1);
let table1 = document.getElementById(clickDay1);
table1.style.backgroundColor = timelineColor;
} else if (dayArray[i] === 'wednesday') {
clickDay = 'wed' + timeArray[i];
let table = document.getElementById(clickDay);
table.style.backgroundColor = timelineColor;
clickDay1 = 'wed' + (Number(timeArray[i]) + 1);
let table1 = document.getElementById(clickDay1);
table1.style.backgroundColor = timelineColor;
} else if (dayArray[i] === 'thursday') {
clickDay = 'tur' + timeArray[i];
let table = document.getElementById(clickDay);
table.style.backgroundColor = timelineColor;
clickDay1 = 'tur' + (Number(timeArray[i]) + 1);
let table1 = document.getElementById(clickDay1);
table1.style.backgroundColor = timelineColor;
} else if (dayArray[i] === 'friday') {
clickDay = 'fri' + timeArray[i];
let table = document.getElementById(clickDay);
table.style.backgroundColor = timelineColor;
clickDay1 = 'fri' + (Number(timeArray[i]) + 1);
let table1 = document.getElementById(clickDay1);
table1.style.backgroundColor = timelineColor;
}
}
Upvotes: 0
Views: 93
Reputation: 7080
You could utilise switch
statement instead of multiple if-elseif
statement:
let timelineColor = getRamdomColor();
for (i = 0; i < dayArray.length; i++){
let day;
switch (dayArray[i]){
case 'monday': day = 'mon'; break;
case 'tuesday': day = 'tue'; break;
case 'wednesday': day = 'wed'; break;
case 'thursday': day = 'tur'; break;
case 'friday': day = 'fri'; break;
}
// I moved these variables into the loop too
let clickDay = day + timeArray[i];
let clickDay1 = day + (Number(timeArray[i]) + 1);
let table = document.getElementById(clickDay);
let table1 = document.getElementById(clickDay1);
table.style.backgroundColor = timelineColor;
table1.style.backgroundColor = timelineColor;
}
Upvotes: 1
Reputation: 107546
You don't really need the if
-statements at all. Set up a map that relates your full day name to the short day names, and look it up:
let timelineColor = getRamdomColor();
let clickDay;
let clickDay1;
var dayMap = {
'monday' : 'mon',
'tuesday' : 'tue',
'wednesday': 'wed',
'thursday' : 'tur',
'friday' : 'fri'
};
for (var i = 0; i < dayArray.length; i++) {
let shortDayName = dayMap[dayArray[i]];
clickDay = shortDayName + timeArray[i];
let table = document.getElementById(clickDay);
table.style.backgroundColor = timelineColor;
clickDay1 = shortDayName + (Number(timeArray[i]) + 1);
console.log(clickDay1);
let table1 = document.getElementById(clickDay1);
table1.style.backgroundColor = timelineColor;
}
Upvotes: 2