question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] - JS Basic Algorithms: Slice and Splice solution should not pass

See original GitHub issue

Describe your problem and how to reproduce it:

While helping a camper with their work on this solution, I took a look at mine and discovered it passes when it should not.

function frankenSplice(arr1, arr2, n) {
  const result = [...arr2.splice(0, n), ...arr1, ...arr2];
  console.log(arr1, arr2)
  return result
}

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

If you paste this code into the editor, you can see that arr2 has been modified from [4, 5, 6] to [5, 6] but the test does not catch this fact.

I believe it may be due to the tests for array modification calling the function without an n parameter:

  - text: The first array should remain the same after the function runs.
    testString: frankenSplice(testArr1, testArr2); assert.deepEqual(testArr1, [1, 2]);
  - text: The second array should remain the same after the function runs.
    testString: frankenSplice(testArr1, testArr2); assert.deepEqual(testArr2, ["a", "b"]);

If I assign a default value of n=1 in the function, then the tests catch this behaviour as expected. As such, I believe updating these function calls in the tests should correct this error.

Add a Link to the page with the problem:

Javascript: Basic Algorithms - Slice and Splice

Tell us about your browser and operating system:

  • Browser Name: Chrome
  • Browser Version: 85.0.4183.102
  • Operating System: Windows 10 v 1909

If possible, add a screenshot here (you can drag and drop, png, jpg, gif, etc. in this box):

image

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
naomi-lgbtcommented, Sep 14, 2020

Yes, updating the test call to:

frankenSplice(testArr1, testArr2, 1)

should correct this bug

0reactions
moT01commented, Sep 14, 2020

I believe updating these function calls in the tests should correct this error.

So, to be clear @nhcarrigan - you are thinking to put a value in for n on those two tests you shared would catch this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Slice and Splice Bug - JavaScript - The freeCodeCamp Forum
My code returns the correct input for every test case (verified via console). I ensured that the 2 original input arrays remain unchanged....
Read more >
Slice and Splice - Basic Algorithm Scripting - Free Code Camp
In this basic algorithm scripting tutorial we do an exercise called slice and splice. This is another tutorial that makes up a series...
Read more >
Solving "Slice and Splice" / freeCodeCamp Algorithm ...
My guide, notes, and solution to freeCodeCamp's basic algorithm ... We will use slice and splice methods to copy first array into the...
Read more >
Base Algorithm Scripting by chopping the array with splice ...
In the code,I have to build in a function, which chop the input array based on the parameter. The testing result should be...
Read more >
Algorithms in JavaScript. 40 Problems, Solutions, and…
Data structures and algorithms go hand-in-hand like Yin and Yang, ... This means it does not care at all about the growth of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found