Was wondering something - Basic Data Structures - Remove Items Using splice()

Hi everyone,

I was wondering if it was possible to make a function which receive an array as a parameter, and apply some splice () to it
Alright i am stupid to want to force a defined array, i shall just write something to splice what was inputed, sorry for the bothering

Solution was :

function displayNumbers(arr) {
arr.splice(4,2);
return arr;
}

console.log(displayNumbers([1,2,3,4,5,6,7,8,9,10]));

god i’m stupid
Your code so far

const arr = [2, 4, 5, 1, 7, 5, 2, 1];
arr.splice(1,4);
console.log(arr);

I did this so far but i am struggling to find the problem related to it …


function displayNumbers(arr) {
let arro = [];
let prout = arr.splice(4,2);
return 
  }

displayNumbers()

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Basic Data Structures - Remove Items Using splice()

Link to the challenge:

Hi Aesir,

Firstly, don’t be so hard on yourself. Making mistakes is a natural part of the programming learning process.

I hope I understood your problem correctly. Please correct me if I misunderstood. Is the bottom solution the one you want to fix? If it is, you need to return prout and you need to pass a parameter when calling displayNumbers() function. Lastly why did you define an empty array named arro?

I think you have a problem because when you assign the array you are applying splice() function to a variable, only the cut part is assigned to this variable.

If we fix the syntax errors and run the code below,

function displayNumbers(arr) {
let prout = arr.splice(4,2);
return prout;
}

displayNumbers([1,2,3,4,5,6,7,8,9,10])

The output will be [5, 6]

I hope it helped. If not, can you explain what they want to do and my faults?

Hi codesompe,
Thanks for the kind words ! I still find myself struggling a lot on simple functions, still have a looong way to go i guess !

I was declaring a value while i didn’t really need it, and took the wrong path to solve my question, i found the solution afterwards but i still lack the method behind.

Actually, your solution was exactly what i was in a need for, thanks ! :slight_smile:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.