1

I need to create an array with the days of the week, and I need to create a function that takes a day and a number n and return the day after the n days (for example getDayAfter('Monday', 4) will return 'Friday').

I've already created my array named day. It has in the array the days of the week.

I'm creating a function called RepeatArray and it's supposed to create a new array.

let day = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];

function repeatArray(day, count) {

  var ln = day.length;
  var b = new Array();
  var a = new Array();
  
  for (i = 0; i < count; i++) {
    day.push(day[i % ln]);
  }

  return b;
}

var a = new Array();
a.push("test1");
a.push("test2");
a.push("test3");

var b = repeatArray(a, 13);

console.log(b.length);

I expect the output of a, 13 to be monday, but the output is 0.

5
  • 3
    day.push shouldn't be b.push? Since you create b but never uses it and then you return it... also I would suggest to change the let day to another name to not mess names with parameter day Commented Jul 17, 2019 at 20:09
  • return b but b is just an empty array? Commented Jul 17, 2019 at 20:09
  • There is no question. What do you want to do? Commented Jul 17, 2019 at 20:16
  • 1
    You should consider using more meaningful variable names. You've got too many as and bs and it makes it harder to reason about what's in which a for example. When you're just starting out, making everything named uniquely and meaningfully will make it harder to introduce meaningless variables (like the b variable in your function. Also note that day is an array of day names, but also the name of an argument to your function; use console.log(day) at the top of your function and you may be surprised by what it contains. I encourage you to use a debugger to step through your code Commented Jul 17, 2019 at 20:17
  • Why are you creating new arrays? Wasn’t the assignment just to get a day, n days after another day? Commented Jul 17, 2019 at 20:17

4 Answers 4

6

Let's walk through what your code currently does, which is often a good way to find any problems.

let day = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];

This creates your days of the week array in an outer scope.

function repeatArray(day, count) {

This declares the repeatArray function -- which shadows the "day" variable. Since it has the same name as the "day" array in the global scope, anything inside this function that references "day" will reference this argument and you will have no access to the outer array.

var ln = day.length;
var b = new Array();
var a = new Array();

ln is now the length of the first argument passed to the function. b and a are empty arrays.

for (i = 0; i < count; i++) {
  day.push(day[i % ln]);
}

This pushes to the first argument the value in the first argument, at the position i mod ln, for all i from 0 to count. So if the first argument is the array ['a', 'b', 'c'], and count is 6, it would be pushing 'a', 'b', 'c', 'a', 'b', 'c' to the first argument, resulting in the array ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']. I don't think this is what you want. But let's continue.

return b;

The function returns b, which is still just a new, empty array with nothing in it, since we haven't touched it.

var a = new Array();
a.push("test1");
a.push("test2");
a.push("test3");

This creates an array, a, which ends up being ['test1', 'test2', 'test3'].

var b = repeatArray(a, 13);
console.log(b.length);

In our function call, 'day' will be the array a (so ['test1', 'test2', 'test3']), and count will be 13. Since the function returns an empty array every time, the length will always be 0. The array a, however, will be modified to repeat 'test1', 'test2', 'test3' 13 extra times, making its length 16 (13 plus the initial 3 values).

For your task, you don't need that kind of repetition or to create a new array. You can just use modular math :)

let day = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
function daysAfter(initial, n) {
    const initialIndex = day.indexOf(initial);
    return day[(initialIndex + n) % day.length];
}

So now daysAfter('wednesday', 13) will equal tuesday as expected :)

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

2 Comments

Great answer, keep writing those :)
Thanks! you saved me.
1

I'm not quite sure what you need repeatArray for, but if you are just trying to, as you say, 'create a function that takes a day and a number n and return the day after the n days' this is how I would do it:

const days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];

function getDayAfter(day, count) {
  const i = days.indexOf(day);
  return days[(i + count) % days.length];
}

console.log(getDayAfter('tuesday', 2)); // Should output 'thursday'

3 Comments

With this, getDayAfter('sunday', 10) will return undefined.
Thanks, this is pretty useful to solve part of the problem <3 but with this, if i don't create the repeatarray it will return undefined if I type something like getDayafter('saturday',5)
@mariettabell yes, other users pointed that out to me. I've now accepted an edit that fixes that.
0

I do not really understand the question but this should work.

    let weekday = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];

    function getDayAfter(day, count) {
      var id = weekday.indexOf(day);
      var ln = weekday.length;
      var targetday = id + count;

      if((id + count) < ln){
          return weekday[id + count];
      }else{
          return weekday[(id + count) % ln];
      }
    }

    var b = getDayAfter("monday", 13);
    document.writeln(b);

Comments

0

Here is the solution I came up with:

const days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'],
      length = days.length;

function getDayFrom(day, count){
    //i % 7 will effectively repeat 0 thru 6 for any value of i
    //creating an array of repeated days. The count is the index of the day
    //expected
    return days[(days.indexOf(day) + count) % length]
    //The above is the efficient version of
    //Array.from({length: count + 1}, (_, i) => days[i % 7])[count]

}
console.log(getDayFrom('monday', 4))

The output is 'friday'.

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.