Skip to content

Instantly share code, notes, and snippets.

@HeyMarcy
Created April 4, 2017 00:13
Show Gist options
  • Select an option

  • Save HeyMarcy/e8eafe43c2e854c2b20aa7f11d511e37 to your computer and use it in GitHub Desktop.

Select an option

Save HeyMarcy/e8eafe43c2e854c2b20aa7f11d511e37 to your computer and use it in GitHub Desktop.

Revisions

  1. HeyMarcy created this gist Apr 4, 2017.
    71 changes: 71 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    // Count Sheep

    var countSheep = function(n){
    if (n === 0){
    return;
    }
    console.log("Another sheep jumps over the fence " +n);
    countSheep(n-1)
    }

    countSheep(12);

    //////////////////////////////////////////////
    // Double Arr

    // Write a program that takes an array as input which contains an unknown set of numbers,
    // and output an array which doubles the values of each item in that array.


    function doubleArr(arr){
    if(!arr.length){
    return [];
    }
    return [ arr[0]*2, ...doubleArr(arr.slice(1)) ];
    }
    console.log(doubleArr([1,2,3,4,5,6]))

    //////////////////////////////////////////////

    // reverses a string.
    // Take a string as input, reverse the string, and return the new string.

    reverse = (string) => {
    if (!string.length){
    return "";
    }
    const lastLtr = string.slice(-1);
    console.log(lastLtr)
    console.log(string.slice(0,string.length-1))

    return lastLtr + reverse(string.slice(0,string.length-1))
    }
    console.log(reverse ("ABCDEFG"));

    //////////////////////////////////////////////
    // Triangle Number

    const f = function(n) {

    if (n === 1) {
    return 1;
    }
    console.log(n)

    //////////////////////////////////////////////
    // String Splitter

    function split(str) {
    if(!str.length) {
    return "";
    }
    return str[0] + split(str.slice(1));

    }
    console.log(split("hello"))
    return n + f( n - 1);
    };

    console.log(f(4));


    59 changes: 59 additions & 0 deletions recursion.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    function split(str) {
    if(!str.length) {
    return "";
    }
    return str[0] + split(str.slice(1));


    }

    //Exercise 5 - String Splitter

    function split(str) {
    if(!str.length) {
    return;
    }
    return str[0] + "-" + split(str.slice(1));


    }

    console.log(split("hello"))

    //Binary


    function binary(num) {
    if (num < 2){
    return num;
    } else {
    return binary(Math.floor(num/2)).toString() + (num % 2).toString();
    }
    }

    binary(37);

    //Factorial

    var factorial = x => {
    if (x === 0) {
    return 1;
    }
    return x * factorial(x-1);
    }

    console.log(factorial(3))

    //Fibonacci

    vvar fibonacci = x => {

    if (x <= 1) {
    return 1
    }
    return fibonacci(x - 1) + fibonacci(x - 2);
    }

    for (i=0; i<20; i++){
    console.log(fibonacci(i));
    }