Skip to content

Commit

Permalink
JavaScript refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rtoal committed Aug 10, 2024
1 parent 173bbc7 commit a651b11
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
6 changes: 3 additions & 3 deletions javascript/exercises.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export function change(amount) {
if (amount < 0) {
throw new RangeError("Amount cannot be negative")
}
let [coins, remaining] = [[], amount]
let [counts, remaining] = [{}, amount]
for (const denomination of [25, 10, 5, 1]) {
coins.push(Math.floor(remaining / denomination))
counts[denomination] = Math.floor(remaining / denomination)
remaining %= denomination
}
return coins
return counts
}

// Write your first then lower case function here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
// powersGenerator,
// fileStats,
// Quaternion,
} from "../exercises.js"
} from "./exercises.js"

describe("The change function", () => {
it("throws for fractions of cents", () => {
Expand All @@ -17,16 +17,16 @@ describe("The change function", () => {
throws(() => change(-50), /RangeError/)
})
it("works for 0", () => {
deepEqual(change(0), [0, 0, 0, 0])
deepEqual(change(0), { 25: 0, 10: 0, 5: 0, 1: 0 })
})
it("works for the usual cases", () => {
deepEqual(change(1), [0, 0, 0, 1])
deepEqual(change(99), [3, 2, 0, 4])
deepEqual(change(42), [1, 1, 1, 2])
deepEqual(change(1), { 25: 0, 10: 0, 5: 0, 1: 1 })
deepEqual(change(99), { 25: 3, 10: 2, 5: 0, 1: 4 })
deepEqual(change(42), { 25: 1, 10: 1, 5: 1, 1: 2 })
})
it("can handle really big values", () => {
deepEqual(change(100000000037), [4000000001, 1, 0, 2])
deepEqual(change(10000000000005), [400000000000, 0, 1, 0])
deepEqual(change(100000000037), { 25: 4000000001, 10: 1, 5: 0, 1: 2 })
deepEqual(change(10000000000005), { 25: 400000000000, 10: 0, 5: 1, 1: 0 })
})
})

Expand Down
4 changes: 1 addition & 3 deletions javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
"main": "exercises.js",
"type": "module",
"scripts": {
"test": "node --test tests/exercises.test.js"
"test": "node --test exercises.test.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "JavaScript Homework Solutions"
}

0 comments on commit a651b11

Please sign in to comment.