File tree Expand file tree Collapse file tree 1 file changed +7
-5
lines changed
lesson_07/conditionals/src Expand file tree Collapse file tree 1 file changed +7
-5
lines changed Original file line number Diff line number Diff line change @@ -115,10 +115,11 @@ export function convertGpaToLetterGrade(gpa: number): string {
115
115
* @return The factorial of n.
116
116
*/
117
117
export function computeFactorial ( n : number ) : number {
118
- let fact = 0
118
+ let fact = 0 ;
119
119
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 ) {
122
123
fact *= i ;
123
124
}
124
125
return fact ;
@@ -151,15 +152,16 @@ export function addNumbers(values: number[]): number {
151
152
*/
152
153
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
153
154
const list : number [ ] = [ ] ;
155
+ const indices = Array . from ( { length : n - 1 } , ( _ , index ) => index + 2 ) ;
154
156
if ( n >= 1 ) {
155
157
list . push ( 0 ) ;
156
158
}
157
159
if ( n >= 2 ) {
158
160
list . push ( 1 ) ;
159
161
}
160
162
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 ] ;
163
165
}
164
166
}
165
167
return list ;
You can’t perform that action at this time.
0 commit comments