-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58c22b1
commit 4a1c004
Showing
3 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export function reverse(string) { | ||
return string.split('').reverse().join('') | ||
} | ||
|
||
export function average(array) { | ||
if (!array.length) { | ||
return 0 | ||
} | ||
|
||
return array.reduce((sum, item) => sum + item, 0) / array.length | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import assert from 'node:assert' | ||
import { describe, test } from 'node:test' | ||
import { average, reverse } from './test-demo.js' | ||
|
||
describe('reverse', () => { | ||
test('reverse of a', () => { | ||
const actual = reverse('a') | ||
const expected = 'a' | ||
assert.strictEqual(actual, expected) | ||
}) | ||
|
||
test('reverse of react', () => { | ||
const actual = reverse('react') | ||
const expected = 'tcaer' | ||
assert.strictEqual(actual, expected) | ||
}) | ||
|
||
test('reverse of saippuakauppias', () => { | ||
const actual = reverse('saippuakauppias') | ||
const expected = 'saippuakauppias' | ||
assert.strictEqual(actual, expected) | ||
}) | ||
}) | ||
|
||
describe('average', () => { | ||
test('of one value is the value itself', () => { | ||
assert.strictEqual(average([1]), 1) | ||
}) | ||
|
||
test('of many items is calculated right', () => { | ||
assert.strictEqual(average([1, 2, 3, 4, 5, 6]), 3.5) | ||
}) | ||
|
||
test('of empty array is zero', () => { | ||
assert.strictEqual(average([]), 0) | ||
}) | ||
}) |