Skip to content

Commit 3138d85

Browse files
author
jjcapparell
committed
converted for statments into for-of
1 parent 588e812 commit 3138d85

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,11 @@ export function convertGpaToLetterGrade(gpa: number): string {
115115
* @return The factorial of n.
116116
*/
117117
export function computeFactorial(n: number): number {
118-
let fact = 0
118+
let fact = 0;
119119
if (n>0){
120-
fact = 1
121-
for (let i=n; i>=0; i--){
120+
fact = 1;
121+
const numbers = Array.from({ length: n }, (_, index) => index + 1);
122+
for (const i of numbers) {
122123
fact *= i;
123124
}
124125
return fact;
@@ -151,15 +152,16 @@ export function addNumbers(values: number[]): number {
151152
*/
152153
export function getFirstNFibonacciNumbers(n: number): number[] {
153154
const list: number[] = [];
155+
const indices = Array.from({ length: n - 1 }, (_, index) => index + 2);
154156
if (n>=1){
155157
list.push(0);
156158
}
157159
if (n>=2){
158160
list.push(1);
159161
}
160162
if (n>=3){
161-
for (let i=2; i<=n; i++){
162-
list[i] = list[i-1] + list[i-2];
163+
for (const i of indices) {
164+
list[i] = list[i - 1] + list[i - 2];
163165
}
164166
}
165167
return list;

0 commit comments

Comments
 (0)