Giter Site home page Giter Site logo

recursion-exercises's Introduction

Practice recursion in JavaScript

What is recursion? When a function calls itself.

Take a look at this repeat function which yields a string of strings repeated specified number of times. Recursive function definition has a base or terminating case (without recurring) and recursive case.

function repeat(num, char) {

  // recursive case
  if (num - 1 > 0) {
    return char + repeat(num - 1, char);
  }
  // base case
  else {
    return char;
  }
}

repeat(3, 'Hey'); // 'HeyHeyHey'

Take a look at the test.js file in each directory to see what needs to be implemented, write an index.js as the solution file.

  • Run all tests from the root: npm test
  • Run a single test from the directory: npm test
  • Run a single test in TDD mode from the directory: npm run tdd

recursion-exercises's People

Contributors

roman01la avatar

Stargazers

Georgi Nikolov avatar vignesh anand avatar Kiran Gudla avatar Gyanendra Chaubey avatar Andrii Ferenchuk avatar  avatar Ken Thai avatar Dmitriy avatar  avatar Wojciech Gucik avatar Duncan avatar Taylor Hurt avatar Lionel avatar Ameno avatar Joseph Curtis avatar nocabbages avatar PANKAJ KUMAR  avatar Gokulakrishnan Kalaikovan avatar Umar Hansa avatar Vasa avatar Uladzimir Havenchyk avatar vaibhav avatar Dima Valuev avatar Mohammed Irfan avatar Domenico Solazzo avatar Eirik L. Vullum avatar Danilo Gasques avatar Julio César avatar Serhii Martyniuk avatar Artem Tyurin avatar Philipp Andreychev avatar Vladimir Rodkin avatar Yevgeny avatar Alejandro Andrés avatar

Watchers

Vladimir Rodkin avatar James Cloos avatar  avatar  avatar

recursion-exercises's Issues

code not working as expected

why this code doesn't work on page, while working in the dev console @ the same time?

function sum () {
  return (function sum_iter (arr, a) {
    var a=a||0;
    if (!arr.length)
      return a;
    a+=arr.pop();
    return sum_iter(arr, a)
  })([].splice.call(arguments,0))
}

screen shot 2015-10-16 at 3 18 14 pm

Arrow functions do not seem to work

@roman01la...

For the "Sum" exercise, the following is accepted...

function sum(first, ...rest) {
    return first + (rest.length === 0 ? 0 : sum(...rest));
}

But otherwise identical code using an arrow function is rejected with a "Not recursive!" error...

const sum = (first, ...rest) => {
    return first + (rest.length === 0 ? 0 : sum(...rest));
}

My preferred, but also rejected, solution was simply...

const sum = (first, ...rest) => 
    first + (rest.length === 0 ? 0 : sum(...rest));

I am quite impressed with the interactive exercise format. I think I might nick a bit of your code.

Kind regards,

Stephen

fibonacci sequence

The fibonacci sequence of 5 should be [0,1,1,2,3,4,5]
https://www.mathsisfun.com/numbers/fibonacci-sequence.html

I'm trying to add my code as below but it's being rejected by the test
function fibonacci(num){
if (num === 0){
return [0];
}else if(num === 1){
return [0,1]
}else{
let list = fibonacci(num-1);
list.push(list[num-1] + list[num-2]);
return list;
}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.