0

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.

4
  • a loop wont help if you need the results in day1, day2 etc Commented Oct 6, 2015 at 13:26
  • 1
    Yes it will: day[n] Commented Oct 6, 2015 at 13:26
  • but day[n] isn't day1, day2, day3 etc - how much other code will need to be rewritten? Commented Oct 6, 2015 at 13:27
  • 1
    that depends on scope and what is "global". but that's a bad coding style anyway :-) dayX should be days = [] Commented Oct 6, 2015 at 13:28

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Good solution, but what if he can't use an array and needs the results as day1, day2?
@RudieVisser That can be done by using window['day' + counter] = formattedString, but this will add the members in Global Variable, then they can be used as day1, day2, ...
1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.