Skip to content

Commit 9fab4e6

Browse files
authored
palindrome-products: fix spec (#178)
1 parent 047e2f5 commit 9fab4e6

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

exercises/practice/palindrome-products/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "@exercism/wasm-palindrome-products",
33
"description": "Exercism exercises in WebAssembly.",
4+
"version": "0.0.1",
45
"author": "Alex Lohr",
56
"type": "module",
67
"private": true,

exercises/practice/palindrome-products/palindrome-products.spec.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,15 @@ beforeAll(async () => {
1414
}
1515
});
1616

17-
const pairs = (paired, item) => {
18-
if (paired.at(-1)?.length === 1) {
19-
paired.at(-1).push(item);
20-
} else {
21-
paired.push([item]);
22-
}
23-
return paired;
17+
const collectPairs = (items) => {
18+
const itemsIterator = items[Symbol.iterator]();
19+
return Array.from({ length: Math.ceil(items.length / 2) },
20+
() => [itemsIterator.next().value, itemsIterator.next().value]);
2421
}
2522
const getPalindromeProduct = ({ func, minFactor, maxFactor }) => {
2623
const [value, factorsOffset, factorsLength] = currentInstance.exports[func](minFactor, maxFactor);
2724
if (value === -1 && factorsLength === 0) throw new Error('min must be <= max');
28-
const factors = currentInstance.get_mem_as_u32(factorsOffset, factorsLength).reduce(pairs, []);
25+
const factors = collectPairs(currentInstance.get_mem_as_u32(factorsOffset, factorsLength));
2926
return { value: value || null, factors };
3027
}
3128
const Palindromes = {

0 commit comments

Comments
 (0)