Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // function sumOf(list){
- // if(list.length === 1){
- // return list[0];
- // }
- // return list[0] = sumOf(list.slice(1));
- // }
- //
- // let lst = [2,4,6,8,10];
- // console.log(sumOf(lst));
- //
- function countingSheep(num){
- if(num > 0){
- console.log(num + 'another sheep jumps over the fence');
- return countingSheep(num-1);
- }
- }
- console.log(countingSheep(5));
- //double each number in an array recursively
- function doubleArray(arr, index = 0, newArr = []){
- if(index === arr.length-1){
- return newArr;
- }
- newArr.push(arr[index] * 2);
- console.log('it is firing here');
- return doubleArray(arr, index + 1, newArr);
- }
- let lst = [2,4,6,8,10];
- console.log(doubleArray(lst, 0))
- function fib(n){
- if(n < 2){
- return n;
- }
- return fib(n-1) + fib(n-2);
- }
- function reverseString(str, i = str.length-1, newStr = ''){
- if(i === 0){
- return newStr;
- }
- newStr += str[i];
- return reverseString(str, i-1, newStr)
- }
- // console.log(reverseString('hello'));
- function triangleNumber(row){
- if(row === 1){
- return 0;
- }
- return row + triangleNumber(row-1);
- }
- console.log(triangleNumber(3));
- ///create a split function using recursion
- function strSplitter(str, splitOn, index = 0, newStr1='', newStr2=''){
- if(index === str.length){
- let anArr = [];
- anArr.push(newStr1, newStr2)
- return anArr;
- }
- if(index < splitOn){
- newStr1 += str[index]
- } else if(index >= splitOn){
- newStr2 += str[index]
- }
- return strSplitter(str, splitOn, index + 1, newStr1, newStr2);
- }
- console.log(strSplitter('Hello', 3));
- function factorial(num){
- if(num === 1){
- return num;
- }
- return num * factorial(num-1);
- }
- console.log(factorial(5));
- //anagrams
- let genAnagrams = (word, anagram = '', anagrams = []) => {
- anagrams.push(anagram);
- for (let i = 0; i < word.length; i++){
- genAnagrams(word.slice(0, i) + word.slice(i + 1), anagram + word[i], anagrams);
- }
- return [...new Set(anagrams)];
- };
- console.log(genAnagrams('ABC'));
- function getAllPermutations(string) {
- var results = [];
- for (var i = 0; i < string.length; i++) {
- var firstChar = string[i];
- var charsLeft = string.substring(0, i) + string.substring(i + 1);
- var innerPermutations = getAllPermutations(charsLeft);
- for (var j = 0; j < innerPermutations.length; j++) {
- results.push(firstChar + innerPermutations[j]);
- }
- }
- return results;
- }
- // console.log(getAllPermutations('abc'));
- const chart = ['Zuckerburg', ['Schoefer', ['Bosworth', ['Steve', 'Kyle', 'Andrea']], ['Zhao', ['Richie', 'Sofia', 'Jen']]],
- ['Schrage', ['VanDyck', ['Sabrina', 'Michelle', 'Josh']], ['Swain', ['Blanch', 'Tom', 'Joe']]],
- ['Sandberg', ['Goler', ['Eddie', 'Julie', 'Annie']], ['Hernandez', ['Rowi', 'Inga', 'Morgan']], ['Moissinac', ['Amy', 'Chuck', 'Vinni']], ['Kelley', ['Eric', 'Anna', 'Wes']]]
- ]
- function printChart(chart, index = 0, indent = ''){
- for(let i = 0; i < chart.length; i++){
- if(typeof chart[i] === 'string'){
- console.log(indent + chart[i]);
- } else {
- printChart(chart[i], index++, indent +=' ')
- }
- }
- }
- console.log(printChart(chart));
- /////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement