1

The following coding problem has me stumped. There are four tests, which begin with "// ex." They are followed by my code. My code is only passing the first test. I am unsure why it is failing the next 3. Thanks in advance for any insight.

// 1.4 repeat(n, array)
// Write a function that takes a non-negative integer n and an array and           returns a new
// array that contains the contents of given array repeated n times.
// ex. repeat(0, [1]) -> []
// ex. repeat(10, []) -> []
// ex. repeat(1, [1, 2, 3]) -> [1, 2, 3]
// ex. repeat(3, [1, 2, 3]) -> [1, 2, 3, 1, 2, 3, 1, 2, 3]

toolbox.repeat = function(n, array) {
  var arr = [];
  for(var i = 0; i < n; i++){
    arr.push(array);
  }
  return arr;
}
2
  • You can print out the result array to some where and see the different. It's basic problem. Commented Oct 14, 2016 at 3:06
  • You're creating an array of arrays, but the function is supposed to return an array of primitives. Commented Oct 14, 2016 at 3:14

4 Answers 4

5

Your problem is this line:

arr.push(array);

What this does is pushes the entire array as a single element, so you get:

[ [], [], ... n times ]

But what you want to do is push the contents of the array, n times.

One way you can solve this is to, instead of pushing, you concat array to arr on each iteration:

arr = arr.concat(array);

Another way would be to call Array.prototype.push and pass arr as the context into which to push and array as the arguments to be pushed:

Array.prototype.push.apply(arr, array);

Or in ES6 you can use the spread operator to do the same:

arr.push(...array)

As self demonstrated, you can also create an inner loop to, for every n, iterate over array and add its elements to arr.

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

2 Comments

This is certainly maybe more idiomatic. But I think it hides the implementation which may not be the best for this exercise.
yeah, that makes sense. I guess it depends on the exact purpose of the exercise but that's why I referenced your answer as well :)
3

You should use array.concat instead

var repeat = function(n, array) {
  var arr = []
  for(var i = 0; i < n; i++){
    arr = arr.concat(array);
  }
  return arr;
}

console.log(repeat(3,[1,2,3]))

Comments

1

It looks like you need to push every value of array onto your new array n times. So you'll have an inner loop of:

var repeat = function(n, array) {
  var arr = [];
  for(var i = 0; i < n; i++) {
    for(var j = 0; j < array.length; j++) {
        arr.push(array[j]);
    }
  }
  console.log(arr);
}

repeat(0, [1])
repeat(10, [])
repeat(1, [1, 2, 3])
repeat(3, [1, 2, 3])

4 Comments

Unfortunately this won't work, OP needs to add the entire array to arr n times, not just attempt to copy contents once (assuming n is within bounds)
oh, you meant two loops, got ya, my bad.
Check my edit, i didn't realize he didn't have the first loop
Both of these methods were extremely helpful, thank you to everyone
0

your code is incorrect, please change a little bit to

toolbox.repeat = function(n, array) {

  var arr = [];

  for(var i = 0; i < n; i++) {
    // use concat array instead
    arr = arr.concat(array);

    return arr;
}

// repeat(0, [1]) -> []
// repeat(10, []) -> []
// repeat(1, [1, 2, 3]) -> [1, 2, 3]
// repeat(3, [1, 2, 3]) -> [1, 2, 3, 1, 2, 3, 1, 2, 3]

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.