1

I am trying to write a function called slice that accepts an array and two numbers.

  • The function should return a new array with the elements starting at the index of the first number and going until the index of the second number.

  • If a third parameter is not passed to the function, it should slice until the end of the array by default.

  • If the third parameter is greater than the length of the array, it should slice until the end of the array.

function slice(s, n, m) {
  let a = [];
  a = s.splice(n, m);
  if(m === undefined || m > s.length) {
    a = s.splice(n, s.length);
  }
  return a;
}

let s = [1, 2, 3, 4, 5];
slice(s, 1, 7);

output []

7
  • 2
    I would recommend using descriptive variable names rather than single characters. Commented Feb 21, 2020 at 22:06
  • 1
    @clark those are not mutually exclusive. Commented Feb 21, 2020 at 22:07
  • 1
    agree with clark that you need to do this on your own. However, I can give you hints: have a pointers in the array to second and third parameters. Commented Feb 21, 2020 at 22:08
  • 3
    Welcome to SO! It's OK to ask about homework in any event, but the issue here is that no question is being asked. OP, can you write a bit more about your attempt? Why did you use splice, for example? What is your thought process? Please ask a concrete question about your code. Thanks! Commented Feb 21, 2020 at 22:09
  • 2
    Start again :-) the splice function modifies an array in place but slice doesn't - so you can't use splice to reinvent slice. The exercise probably wanted you to use loops and some logic tests for elements to be placed in a new array. Commented Feb 21, 2020 at 22:10

2 Answers 2

2

The main problem is that .splice mutates the array, so on your first call to it you remove a part of the array (or in your case the whole array). Therefore if the code enters the if, and you call .splice a second time, the array is already empty. An if / else would work so that .splice gets only called once.

But that would still then not replicate the behaviour of .slice, as .slice does not mutate the original array. Therefore you rather need a loop, that copies one element after the other:

 // if "do" doesn't get passed, initialize with array.length (default parameter)
 function slice(array, from, to = array.length) {
   // make sure the bounds are within the range
   from = Math.max(from, 0);
   to = Math.min(to, array.length);
   // initialize an array we can copy values into
   const result = [];
   for(let index = from; index < to; index++) {
     // left as an exercise :)
   }
   return result;
 }
Sign up to request clarification or add additional context in comments.

Comments

1

Answering this since the OP said the time for the homework has passed.

One way to solve this problem is to have two pointers to second and third arguments. If you have all the arguments given, this is how you should start

start = n
end = m

// if m is greater than length of s or if m is not given
if(m == undefined || m > s.length()){
    end = s.length() - 1;
}

then it is a simple for loop from start to end, both inclusive.

int[] result = new int[end-start+1];
for(int i = start; i <= end; i++){
    result[j] = s[i];
}

Code may not be syntactically correct but you can fix that.

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.