-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path12-iterative-version.js
More file actions
155 lines (102 loc) · 4.62 KB
/
Copy path12-iterative-version.js
File metadata and controls
155 lines (102 loc) · 4.62 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
// Solve the drills 1 - 7 from your previous checkpoint (Recursion) iteratively.
// 1. Counting Sheep
// Write a recursive function that counts how many sheep jump over the fence.
// Your program should take a number as input. That number should be the number of sheep you have.
// The function should display the number along with the message "Another sheep jumps over the fence" until no more sheep are left.
// Input: 3
// Output:
// 3: Another sheep jumps over the fence
// 2: Another sheep jumps over the fence
// 1: Another sheep jumps over the fence
// All sheep jumped over the fence
const sheepCount = (int) => {
let start = int
for (let i = start; i <= start; i--) {
if (i === 0){ return ('All the sheep have jumped over the fence') }
else console.log(`Sheep number ${i} jumps over the fence`)
}
}
console.log(sheepCount(3))
//------------------------------------------------------------------------------------//
// 2. Power Calculator
// Write a function called powerCalculator() that takes two parameters, an integer as a base, and another integer as an exponent.
// The function returns the value of the base raised to the power of the exponent.
// Use only exponents greater than or equal to 0 (positive numbers)
// powerCalculator(10,2) should return 100
// powerCalculator(10,-2) should return exponent should be >= 0
const powerCalculator = (number, exponent) => {
if (exponent < 0){
return 'Exponent should be >= 0'
} else if (exponent === 0){
return 1
}
return number ** exponent
}
console.log(powerCalculator(10, 2))
//------------------------------------------------------------------------------------//
// 3. Reverse String
// Write a function that reverses a string. Take a string as input, reverse the string, and return the new string.
const reverseString = (string) => {
return string.split("").reverse().join("")
}
console.log(reverseString("Elijah"))
//------------------------------------------------------------------------------------//
// 4. nth Triangular Number
// Calculate the nth triangular number.
// A triangular number counts the objects that can form an equilateral triangle.
// The nth triangular number is the number of dots composing a triangle with n dots on a side, and is equal to the sum of the n natural numbers from 1 to n.
// This is the Triangular Number Sequence: 1, 3, 6, 10, 15, 21, 28, 36, 45.
const nth = (number) => {
let start = number
for (let i = number - 1; i <= number; i--) {
if(i === 0) {return start}
else{start += i}
}
}
console.log(nth(8))
//------------------------------------------------------------------------------------//
// 5. String Splitter
// Write a recursive function that splits a string based on a separator (similar to String.prototype.split).
// Don't use JS array's split function to solve this problem.
// Input: 02/20/2020
// Output: ["02", "20", "2020"]
var stringSpliter = function (string, seperator) {
let stringArray = [''];
let j = 0;
for (let i = 0; i < string.length; i++) {
if (string.charAt(i) == seperator) {
j++;
stringArray.push('');
} else {
stringArray[j] += string.charAt(i);
}
}
return stringArray;
}
console.log(stringSpliter('02/20/2020', '/'))
//------------------------------------------------------------------------------------//
// 6. Fibonacci
// Write a recursive function that prints the Fibonacci sequence of a given number.
// The Fibonacci sequence is a series of numbers in which each number is the sum of the 2 preceding numbers.
// For example, the 7th Fibonacci number in a Fibonacci sequence is 13. The sequence looks as follows: 1, 1, 2, 3, 5, 8, 13.
const fibonacci = (number) => {
const result = [1, 1]
for (let i = 2; i <= number; i++) {
result.push(result[i - 1] + result[i - 2])
} return result
}
console.log(fibonacci(8))
//------------------------------------------------------------------------------------//
// 7. Factorial
// Write a recursive function that finds the factorial of a given number.
// The factorial of a number can be found by multiplying that number by each number between itself and 1.
// For example, the factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120.
const factorial = (number) => {
if (number <= 0) return ('Must be 1 or more')
for (let i = number - 1; i >= 1; i--) {
number *= i
}
return number
}
console.log(factorial(5))
//------------------------------------------------------------------------------------//