Skip to content

Commit 1051fd2

Browse files
committed
added prefix sum code and fix prettier
1 parent bec2ff0 commit 1051fd2

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

Data-Structures/Array/BasicPrefixSum.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
*/
88
export const basicPrefixSum = (arr) => {
99
if (!Array.isArray(arr)) {
10-
throw new TypeError("Input must be an array");
10+
throw new TypeError('Input must be an array')
1111
}
1212

13-
if (!arr.every((item) => typeof item === "number")) {
14-
throw new TypeError("All elements must be numbers");
13+
if (!arr.every((item) => typeof item === 'number')) {
14+
throw new TypeError('All elements must be numbers')
1515
}
1616

17-
const prefix = [];
18-
let sum = 0;
17+
const prefix = []
18+
let sum = 0
1919
for (const num of arr) {
20-
sum += num;
21-
prefix.push(sum);
20+
sum += num
21+
prefix.push(sum)
2222
}
23-
return prefix;
24-
};
23+
return prefix
24+
}
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import { describe, it, expect } from "vitest";
2-
import { basicPrefixSum } from "../BasicPrefixSum.js";
1+
import { describe, it, expect } from 'vitest'
2+
import { basicPrefixSum } from '../BasicPrefixSum.js'
33

4-
describe("basicPrefixSum", () => {
5-
it("computes prefix sums correctly", () => {
6-
expect(basicPrefixSum([1, 2, 3, 4])).toEqual([1, 3, 6, 10]);
7-
});
4+
describe('basicPrefixSum', () => {
5+
it('computes prefix sums correctly', () => {
6+
expect(basicPrefixSum([1, 2, 3, 4])).toEqual([1, 3, 6, 10])
7+
})
88

9-
it("returns an empty array for empty input", () => {
10-
expect(basicPrefixSum([])).toEqual([]);
11-
});
9+
it('returns an empty array for empty input', () => {
10+
expect(basicPrefixSum([])).toEqual([])
11+
})
1212

13-
it("throws an error if input is not an array", () => {
14-
expect(() => basicPrefixSum("abc")).toThrow(TypeError);
15-
});
13+
it('throws an error if input is not an array', () => {
14+
expect(() => basicPrefixSum('abc')).toThrow(TypeError)
15+
})
1616

17-
it("throws an error if array contains non-numeric elements", () => {
18-
expect(() => basicPrefixSum([1, "2", 3])).toThrow(TypeError);
19-
});
20-
});
17+
it('throws an error if array contains non-numeric elements', () => {
18+
expect(() => basicPrefixSum([1, '2', 3])).toThrow(TypeError)
19+
})
20+
})

0 commit comments

Comments
 (0)