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.

Arguments Optional missing a valid test case that would cause existing Guide solutions to fail

See original GitHub issue

Describe the Issue

I was looking at the existing solutions in the Guide for the Arguments Optional challenge and noticed if we added the following valid test case, both Guide solutions would fail. For this test case, they should return undefined but instead return a function.

Extra Test Case:

addTogether(5, undefined) should return undefined.

I am proposing we add the new test case, add the solution I propose as one of the solutions in the Guide, and refactor/remove the existing solutions.

Existing Guide Solutions:

Solution Number 1

function addTogether(...args) {
  const [first, second] = args;
  if (typeof first === 'number') {
    if (args.length === 1) {
      return num => {
        if (typeof num === 'number') {
         return first + num;
       }
      };
    }
    if (typeof second === 'number') {
      return first + second;
    }
  }
}
function addTogether() {
  const [first, second] = arguments;
  if (typeof(first) !== "number")
    return undefined;
  if (second === undefined)
    return (second) => addTogether(first, second);
  if (typeof(second) !== "number")
    return undefined;
  return first + second;
}

Solution Number 2

function addTogether() {
  const [first, second] = arguments;
  // First argument is not a number
  if (typeof(first) !== "number") {
    return undefined;
  }
  // First argument is a number
  //  and second argument is not defined
  else if (second === undefined) {
    function addSecond(second) {
      // New argument is not a number
      if (typeof(second) !== "number") {
        return undefined;
      }
      // New argument is a number
      else {
        return first + second;
      }
    }
    // Note: returning a *function*
    return addSecond;
  }
  // First argument is a number
  //  and second argument is not a number
  else if (typeof(second) !== "number") {
    return undefined;
  }
  // First argument is a number
  //  and second argument is a number
  else {
    return first + second;
  }
}

Affected Page

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/arguments-optional

Your code

n/a

Expected behavior

Using the following solution, it would pass not only the existing test cases but also the new one I described in the above section.

function addTogether(...args) {
  const [first, second] = args;
  if (args.length === 1 && typeof first === 'number') {
    return num => {
      if (typeof num === 'number') {
        return first + num;
      }
    }
  }
  if (typeof first === 'number' && typeof second === 'number') {
    return first + second;
  }
}

Screenshots

No response

System

  • Device: [e.g. iPhone 6, Laptop]
  • OS: [e.g. iOS 14, Windows 10, Ubuntu 20.04]
  • Browser: [e.g. Chrome, Safari]
  • Version: [e.g. 22]

Additional context

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jeremyltcommented, Jul 27, 2022

Sure, I went ahead and added it

1reaction
RandellDawsoncommented, Jul 26, 2022

I have opened this issue up for contributors.

The requirements for before submitting a PR for this issue are:

  1. Adding the new test case as described in the issue body.
  2. Validate your changes work by running the following command without receiving any errors: npm run test:curriculum --superblock=javascript-algorithms-and-data-structures
  3. Read through the How to Work on Coding Challenges and How to open a Pull Request (PR) and use recommended guidelines for creating the PR title and body content.
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Write Test Cases: The Ultimate Guide with Examples
Writing effective cases is a skill. You can learn it from the experience and knowledge of the application under test. For basic instructions...
Read more >
googletest/advanced.md at main - GitHub
This document will show you more assertions as well as how to construct complex failure messages, propagate fatal failures, reuse and speed up...
Read more >
REST API Testing Strategy: What Exactly Should You Test?
Several methods and resources help with HOW to test APIs — manual testing ... Execute API call with valid required parameters AND valid...
Read more >
Robot Framework User Guide
Enables easy-to-use tabular syntax for creating test cases in a uniform way. Provides ability to create reusable higher-level keywords from the existing ......
Read more >
Run manual tests - Azure Test Plans | Microsoft Learn
Learn about test tools to run manual tests with Azure Test Plans to make sure each of the deliverables meets your user's needs....
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