forked from joshuaNewman10/Recursive-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.js
More file actions
182 lines (152 loc) · 6.54 KB
/
algorithms.js
File metadata and controls
182 lines (152 loc) · 6.54 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// _____ _ ////////////////////
// | __ \ (_) ////////////////////
// | |__) |___ ___ _ _ _ __ ___ _ ___ _ __ ////////////////////
// | _ // _ \/ __| | | | '__/ __| |/ _ \| '_ \ ////////////////////
// | | \ \ __/ (__| |_| | | \__ \ | (_) | | | | ////////////////////
// |_| \_\___|\___|\__,_|_| |___/_|\___/|_| |_| ////////////////////
// ////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// 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){
//declare count variable to keep track of the vowels num
//take the initial character in the string and check if the character is a vowel
if (str.length === 0) {
return 0;
} else if (str[0] === 'a' || str[0] === 'e' || str[0] === 'i' || str[0] === 'o'|| str[0] === 'u') {
return 1 + countVowels(str.slice(1, str.length));
} else {
return countVowels(str.slice(1, str.length));
}
//if it is a vowel
//increase the count in the count variable
//if not,
//call the function on the rest of the string
//return the count variable
};
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// 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){
n = n.toString();
//base case: if no number is given,
if (n.length === 1) {
//return 0;
return Number(n);
} else {
return Number(n.charAt(0)) + recursiveSum(Number(n.slice(1, n.length)));
}
//otherwise, take the last number using modulo 10
//return that digit added with the recursion call
};
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// 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){
//if the min and the max is the same,
if (min === max) {
console.log(min);
} else {
console.log(min) + printRangeUpDown(min + 1, max) + console.log(min);
}
//console.log only the min
//if the range between min and max is a difference of 1
//console.log min, max, max, min
//otherwise,
//console.log min first and then call printRangeUpDown with min + 1, 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 BinaryTree = function(value) {
this.value = value;
this.left = null;
this.right = null;
};
var binaryTreeSum = function(tree){
var sum = tree.value;
if (tree.left !== null && tree.right !== null) {
return sum += binaryTreeSum(tree.left) + binaryTreeSum(tree.right);
} else if (tree.left !== null) {
return sum += binaryTreeSum(tree.left);
} else if (tree.right !== null) {
return sum += binaryTreeSum(tree.right);
} else {
return sum;
}
};
var sum = new BinaryTree(10);
sum.left = new BinaryTree(7);
sum.right = new BinaryTree(11);
sum.left.left = new BinaryTree(2);
console.log(binaryTreeSum(sum));
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// Problem #7
// Given an array of integers which is sorted in increasing order
//[1,2,3,4,5,6,7,8,9]
// 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){
//create an empty binary tree with the first item of the array
var newTree = new BinaryTree(array[0]);
//run through each item of the array
//add the next integer
//return the new binarySearchTree
return newTree;
};