Implement a function “duplicate” to duplicate an array, as shown below:
For input arr=[1,2] duplicate(arr) should return [1,2,1,2]
//Write your function here
function duplicate(arr) {
let array1 = [1, 2];
let array2 = [1, 2];
return arr = array1.concat(array2);
}
console.log(duplicate());
// Input and output has already been handled for you
process.stdin.resume();
process.stdin.setEncoding('utf8');
let remainder = '';
process.stdin.on('data', function (chunk) {
let arr = chunk.toString()
arr = arr.split(' ')
let nums = []
for(const c of arr) {
nums.push(parseInt(c))
}
const ans = duplicate(nums).join(' ')
process.stdout.write(ans)
process.exit();
});
//Write your function here
function duplicate(arr) {
// Change your code here
}
// Input and output has already been handled for you
process.stdin.resume();
process.stdin.setEncoding('utf8');
let remainder = '';
process.stdin.on('data', function (chunk) {
let arr = chunk.toString()
arr = arr.split(' ')
let nums = []
for(const c of arr) {
nums.push(parseInt(c))
}
const ans = duplicate(nums).join(' ')
process.stdout.write(ans)
process.exit();
});
So, this was the original question, which I tried to solve and tried to implement a function “duplicate” to duplicate an array, as shown below:
For input arr = [1, 2] duplicate (arr) should return [1,2,1,2].
I was able to get the answer as displayed above, but, I had to change the function calling method, this makes the solution incorrect.
BASICALLY, they are saying:
Your Output
[ 1, 2, 1, 2 ] 1 2 1 2
Incorrect Output
Expected Output
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
Implement a function “duplicate” to duplicate an array, as shown below:
For input arr=[1,2] duplicate(arr) should return [1,2,1,2]
The code given for me to try making a function is
//Write your function here
function duplicate(arr) {
// Write Logic here
}
Here, “THEY” imply to the question. The question is asking me to make a function that basically clones the array. I am not able to understand how to do that.
I can use arr.splice to add the numbers [1,2] to the already made [1,2].
But how to make it into the function??
You should not write array1 or array2 at all. You should only use arr.
It seems like you are missing some fundamental knowledge.
All the stuff in the ()s for a function are the arguments. Those are the inputs. You need to use the arguments to make the result that the array is supposed to output.
So they will call duplicate([1, 2]) or duplicate([42, 13, 99]), and your function should return the correct result based upon that arr was given to the function.
I am not understanding the “Expected Output” shown above. One, it is not an array and two, I am not sure what the input array is that is supposed to produce this.
Based on the instructions, I would expect your function to take an array (in your case you have accomplished that by having arr as the function parameter). Next, I would expect your function to iterate through arr to figure out what to “duplicate” and add to an array that will be returned by the function.
As @JeremyLT said above, your function should be able to handle any array (not just [1, 2] as a value passed to it).
If you are not able to answer the above question, I might suggest you check out this lesson from freeCodeCamp’s Basic JavaScript course. It does a good job of explaining the basics of values passed to a function and doing something with the value.
We will not directly tell you how to code this problem. We will attempt to guide you to a solution, but you will need to learn it for yourself and attempt to apply what you learn to solve the problem.
If I call the below function like:
function duplicate(arr) {
}
duplicate([1, 2, 3, 4, 5));
I would say, the value of arr here is [1, 2, 3, 4, 5], which is passed by you as argument. But, the code above should not work as there is nothing inside that function.
And thank you so much for your valuable guidance sir.
You are correct about the above example not doing anything. That was just an example to make sure you understood what arr meant.
Based on the instructions, I would expect your function to take an array (in your case you have accomplished that by having arr as the function parameter). Next, I would expect your function to iterate through arr to figure out what to “duplicate” and add to an array that will be returned by the function.
Expanding on what I said before, do you understand how to iterate through an array and access each value of the array? If so, then since you already seem to understand how to combine two arrays using concat, then you just need to create a new array by iterating through the original array and put each value of it into the new array. After that, you would return the combined result of the original array and the new array of duplicates.
So it as long as you understand how to iterate through an array and access each value, the only other thing you need to know how to do is how to put each accessed value into a new array. To do that, you would first need to know how to create an empty array.
See if you can put all this together and if you get stuck, post the latest version of the code you are try to use.
That method works also. There is always more than one way to solve most problems.
The only issue I see with your solution, is that you actually mutate/change the original array by pushing the duplicates to it. It is considered a better practice to not do that.
Using your current logic along with what I originally mentioned in an earlier reply, you could do the following to accomplish that:
function duplicate(arr) {
const dupArr = [];
for (let i = 0; i < arr.length; i++) {
dupArr.push(arr[i]);
}
return arr.concat(dupArr);
}
Actually, a simpler solution is to take advantage of the fact that concat returns a new array, so you can simply write:
function duplicate(arr) {
return arr.concat(arr);
}
I could have guided you to the second solution above, but I wanted you to write more code so you could see more of what is going on instead of just using a built-in function to accomplish the task.