forked from joshuaNewman10/Recursive-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithms.js
124 lines (90 loc) · 4.44 KB
/
algorithms.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// _____ _ ////////////////////
// | __ \ (_) ////////////////////
// | |__) |___ ___ _ _ _ __ ___ _ ___ _ __ ////////////////////
// | _ // _ \/ __| | | | '__/ __| |/ _ \| '_ \ ////////////////////
// | | \ \ __/ (__| |_| | | \__ \ | (_) | | | | ////////////////////
// |_| \_\___|\___|\__,_|_| |___/_|\___/|_| |_| ////////////////////
// ////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// NOTE: modify the parameter list of each function as needed ///
///////////////////////////////////////////////////////////////////////
// Problem #1
// Write a recursive method called countVowels that returns the number of vowels in a given String
// countVowels('abcedfg') ->2
var countVowels = function(str){
};
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// Problem #2
// Given a non-negative int n, return the sum of its digits recursively (no loops)
// sumDigits(126) → 9
// sumDigits(49) → 13
// sumDigits(12) → 3
var recursiveSum = function(n){
};
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// Problem #3
// Check if a given number is a power of 2
// PowerOfTwo(8) -> true
// PowerOfTwo(9) -> false
var isPowerOfTwo = function(n){
};
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// Problem #4
// Write a recursive method that takes as parameters an initial investment amount,
// an annual interest rate, and a number of years.
// The method should return the value of the investment after the given number of years,
// assuming that the interest is compounded annually.
// (For example, if the initial investment is 1000 and the interest rate is 10 percent,
// then after one year the investment will be worth 1100, after two years 1210, after three years 1331, etc.)
var invest = function(amount){
};
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// Problem #5
// given a min and a max, both integers, use recursion to console.log all of the
// integers from the min to the max, and then console.log the numbers from the max
// to the min. Do not use loops! Use recursion.
// ex:
// printRangeUpDown(4, 10);
// console.logs: 4,5,6,7,8,9,10,9,8,7,6,5,4
var printRangeUpDown = function(min, max){
};
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// Problem #6
// given a binary tree where each node has a
// value, a left and a right, return the sum of all of the values.
// remember, binary tree's are different from binary search trees!
// you'll need to create a binary tree constructor!
var binaryTreeSum = function(tree){
};
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// Problem #7
// Given an array of integers which is sorted in increasing order
// build a binary search tree of minimal height. Height of a tree
// is the max number of edges from a node to the tree's root node.
// e.g. this tree has height 3.
// 10
// / \
// 3 30
// / \
// 1 7
// \
// 8
// you'll need to create a binary search tree constructor!
var arrayToBinarySearchTree = function(array){
};