Skip to content

Recursive Methods: Testing with Jest #27265

@nik-rev

Description

@nik-rev

Checks

Describe your suggestion

The website linked for solving recursive problems (9 problems in NodeJS path) (7 problems in Ruby path) would be better suited as an exercise in my opinion that users can fork and test using jest.

NodeJS Path

We can create 9 new exercise folders in javascript-exercises repository that will have a README.md, exercise.js, exercise.spec.js, and a solutions folder containing the solution and the solution spec, similar to the first 12 exercises.

Below is an example of how the first exercise could be added.

Exercise 13 - sumRange

README.md

Implement a recursive function that will take a number and return the sum of all numbers from 1 up to to the number passed in.

sumRange(5) // should add 1 + 2 + 3 + 4 + 5 and return 15
sumRange.js
const sumRange = function() {

};

// Do not edit below this line
module.exports = sumRange;
sumRange.spec.js
const sumRange = require('./sumRange')

describe('sumRange', () => {
  test('Sums numbers in a small range', () => {
    const range = 4;
    expect(sumRange(range)).toBe(10);
  });
  test('Sums numbers in a large range', () => {
    const range = 100;
    expect(sumRange(range)).toBe(5050);
  });
  test('Returns undefined for negative range', () => {
    const range = -3;
    expect(sumRange(range)).toBe(undefined);
  });
  test('Returns undefined for non integer range', () => {
    const range = 3.7;
    expect(sumRange(range)).toBe(undefined);
  });
});
sumRange-solution.js
function sumRange(num){
  if (!Number.isInteger(num) || num < 0) return undefined;

  if(num === 1) return 1;

  return num + sumRange(num - 1);
}

module.exports = sumRange;

Ruby path

Similarly, we can create a new folder in the Ruby path repository ruby-exercises to align with the current structure of that repository called 'ruby_recursive' and place the recursive problems there.

Path

Ruby / Rails, Node / JS

Lesson Url

https://www.theodinproject.com/lessons/ruby-recursive-methods

(Optional) Discord Name

Revenco

(Optional) Additional Comments

No response

Metadata

Metadata

Labels

Status: StaleThis issue/PR has been inactive for over 30 days and will be closed if inactivity continues

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions