I have this array that corresponds to a list of opening and closing times. I've been able to manipulate it to display what I am needing. The array corresponds to open and close times throughout the week. I am able to break up the array and list the times, but I repeat myself often, and I am wondering if there is a better, DRYer, cleaner way of doing this.
var array = ["Today at 8:00 AM", "Today at 4:00 PM",
"Tomorrow at 8:00 AM", "Tomorrow at 4:00 PM", "Thursday at 8:00 AM",
"Thursday at 4:00 PM", "Friday at 8:00 AM", "Friday at 4:00 PM",
"Saturday at 10:00 AM", "Saturday at 8:00 PM", "Sunday at 8:00 AM",
"Sunday at 4:00 PM", "Monday at 8:00 AM", "Monday at 4:00 PM"]
day1 = "Open " + array[0] + " Closed " + array[1]
day2 = "Open " + array[2] + " Closed " + array[3]
day3 = "Open " + array[4] + " Closed " + array[5]
day4 = "Open " + array[6] + " Closed " + array[7]
day5 = "Open " + array[8] + " Closed " + array[9]
day6 = "Open " + array[10] + " Closed " + array[11]
day7 = "Open " + array[12] + " Closed " + array[13]
I will admit that loops has always been my achilles heal when it comes to development. Would a loop be a solution to condensing this code a little bit? If so, how, and what kind of loop would be used?
I would like to list them out by days, similar to what I have in my very long repetitive code.
2 Answers 2
You can use day as array and add the strings into array by using push. And to access day1 use day[0], day2 as day[1] and so on.
var array = [
"Today at 8:00 AM", "Today at 4:00 PM",
"Tomorrow at 8:00 AM", "Tomorrow at 4:00 PM",
"Thursday at 8:00 AM", "Thursday at 4:00 PM",
"Friday at 8:00 AM", "Friday at 4:00 PM",
"Saturday at 10:00 AM", "Saturday at 8:00 PM",
"Sunday at 8:00 AM", "Sunday at 4:00 PM",
"Monday at 8:00 AM", "Monday at 4:00 PM"
];
var day = []; // Declare empty array
for (var i = 0, len = array.length; i < len; i += 2) {
// i is incremented by 2 for each iteration
// Push the formed string into the array
day.push("Open " + array[i] + " Closed " + array[i + 1]);
}
Although, you can use window instead of day in above code and add the variables in the window i.e. global scope as
window['day' + counter] = '...;
this practice is not encouraged.
I'll suggest/recommend you to pass the variable day as parameter to the function where you need to access it and it is outside of the current scope.
2 Comments
day1, day2?window['day' + counter] = formattedString, but this will add the members in Global Variable, then they can be used as day1, day2, ...If you need your days to be day1, ... you could use this code, it simply add's it to an object called week, week would then contain week.day1, week.day2, ...
(you could also choose window instead of week, and then you would also find back day1 ;))
var array = ["Today at 8:00 AM", "Today at 4:00 PM",
"Tomorrow at 8:00 AM", "Tomorrow at 4:00 PM", "Thursday at 8:00 AM",
"Thursday at 4:00 PM", "Friday at 8:00 AM", "Friday at 4:00 PM",
"Saturday at 10:00 AM", "Saturday at 8:00 PM", "Sunday at 8:00 AM",
"Sunday at 4:00 PM", "Monday at 8:00 AM", "Monday at 4:00 PM"
];
var week = window, // if you want it not as a global var, use {} here instead of window
dayProperty = "day";
for (var i = 0, j = 1, len = array.length; i < len; i += 2, j++) {
var target = dayProperty + j;
week[target] = "Open " + array[i] + ", Closes " + array[i + 1];
}
console.log(week);
console.log(day1); // when week is window, then day1 should be defined here (though it doesn't make your code cleaner)
day1,day2etcday[n]dayXshould bedays = []