0

I wanted make [1,2,3,4,5].duplicated() works but when I wrote:

Array.prototype.duplicator = function (){
    var a = [];
    for(var i=0;i<10;i+=2){
    a[i]=this[i];
    a[i+1]=this[i];
}
return a;
};
 [1,2,3,4,5].duplicator();

it returns [1, 1, 3, 3, 5, 5, undefined, undefined, undefined, undefined] instead of [1,1,2,2,3,3,4,4,5,5]. Can anyone tell me why it does not work?

9 Answers 9

5
Array.prototype.duplicator=function()
{
    return  this.concat(this)
}

alert([1,2,3,4,5].duplicator());
Sign up to request clarification or add additional context in comments.

2 Comments

This is the best one.
This isn't quite what the OP is asking for. Your solution will result in: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] Rather than what they want: [1, 1, 2, 2, 3, 3, 4, 4, 5, 5] I presume the person asking this question is trying to solve a question from this link. In which case your solution Olga is correct!
1

You could just map and flatten for a more functional approach:

Array.prototype.duplicate = function() {
  return [].concat.apply([], this.map(function(v) {
    return [v,v];
  }));
};

console.log([1,2,3].duplicate()); //=> [1,1,2,2,3,3]

Comments

1

The simplest answer should be:

Array.prototype.duplicator=function () {
    return this.concat(this).sort();
}

console.log([1,2,3,4,5].duplicator());//[1,1,2,2,3,3,4,4,5,5]

Comments

0

Because you're adding 2 at each iteration, thus stepping beyond the array boundaries. Try this instead:

for (var i=0; i<this.length; i++) {
    a.push(this[i]);
    a.push(this[i]);
}

Comments

0

You would want to place each value in i*2 and i*2+1 instead of i and i+1, and loop one step at a time:

Array.prototype.duplicator = function (){
  var a = [];
  for(var i = 0; i < this.length; i++){
    a[i*2] = this[i];
    a[i*2+1] = this[i];
  }
  return a;
};

Comments

0

Here's a fix:

Array.prototype.duplicator = function() {
  var dup = [];
  for (var i = 0; i < this.length; i++) {
    dup[2 * i] = dup[2 * i + 1] = this[i];
  }
  return dup;
};

console.log([1,2,3,4,5].duplicator());

Comments

0
Array.prototype.duplicator = function () {
    var a = [], k = 0;
    for(var i=0;i<this.length;i++) {
       a[k]=this[i];
       k++;
       a[k]=this[i];
       k++;
    }
    return a;
};

7 Comments

The question was "Can anyone tell me why it does not work?", so please add some explanation.
Simple exaplianation that your code contain bug. At secound iteration your i===2 so this[i] === 3
You should edit the question rather than posting a comment. Stack Overflow's format is Q&A, not discussion thread.
How about to open any programming book, before teach other people how to use Stack Overflow?
@AdiInbar - editing the code in the question is a bad idea. It could hide the cause of the problem. There's nothing wrong with posting an answer that shows the correct code - as long as it includes an explanation.
|
0
duplicator = val => val.concat(val);

Comments

0

This works for me following the case in https://github.com/h5bp/Front-end-Developer-Interview-Questions/blob/master/src/questions/javascript-questions.md, where the desired result is [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]. Hope it works for you!

const duplicate = (...nums) => [].concat(...nums, ...nums);

console.log(duplicate([1, 2, 3, 4, 5]));

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.