Arguments Optional missing a valid test case that would cause existing Guide solutions to fail
See original GitHub issueDescribe 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 returnundefined
.
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
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:
- Created a year ago
- Comments:7 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sure, I went ahead and added it
I have opened this issue up for contributors.
The requirements for before submitting a PR for this issue are:
npm run test:curriculum --superblock=javascript-algorithms-and-data-structures