diff --git a/package-lock.json b/package-lock.json index cde37d56..4ebfdb9c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,18 +1,18 @@ { - "name": "CYF-Coursework-Template", + "name": "Module-JS2", "version": "1.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "CYF-Coursework-Template", + "name": "Module-JS2", "version": "1.0.0", "license": "CC-BY-SA-4.0", "devDependencies": { "@testing-library/dom": "^8.19.0", "@testing-library/jest-dom": "^5.16.5", "@testing-library/user-event": "^13.5.0", - "jest": "^29.2.2", + "jest": "^29.7.0", "jest-environment-jsdom": "^29.2.2" } }, diff --git a/package.json b/package.json index 355f4cf8..21fd3d31 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "Module-JS2", + "name": "module-js2", "version": "1.0.0", "license": "CC-BY-SA-4.0", "description": "You must update this package", @@ -18,12 +18,306 @@ "@testing-library/dom": "^8.19.0", "@testing-library/jest-dom": "^5.16.5", "@testing-library/user-event": "^13.5.0", - "jest": "^29.2.2", + "jest": "^29.7.0", "jest-environment-jsdom": "^29.2.2" }, "jest": { "setupFilesAfterEnv": [ "./jest.setup.js" ] - } + }, + "main": "jest.setup.js", + "dependencies": { + "abab": "^2.0.6", + "acorn": "^8.10.0", + "acorn-globals": "^7.0.1", + "acorn-walk": "^8.2.0", + "agent-base": "^6.0.2", + "ansi-escapes": "^4.3.2", + "ansi-regex": "^5.0.1", + "ansi-styles": "^4.3.0", + "anymatch": "^3.1.3", + "argparse": "^1.0.10", + "aria-query": "^5.1.3", + "array-buffer-byte-length": "^1.0.0", + "asynckit": "^0.4.0", + "available-typed-arrays": "^1.0.5", + "babel-jest": "^29.7.0", + "babel-plugin-istanbul": "^6.1.1", + "babel-plugin-jest-hoist": "^29.6.3", + "babel-preset-current-node-syntax": "^1.0.1", + "babel-preset-jest": "^29.6.3", + "balanced-match": "^1.0.2", + "brace-expansion": "^1.1.11", + "braces": "^3.0.2", + "browserslist": "^4.22.1", + "bser": "^2.1.1", + "buffer-from": "^1.1.2", + "call-bind": "^1.0.2", + "callsites": "^3.1.0", + "camelcase": "^5.3.1", + "caniuse-lite": "^1.0.30001547", + "chalk": "^4.1.2", + "char-regex": "^1.0.2", + "ci-info": "^3.9.0", + "cjs-module-lexer": "^1.2.3", + "cliui": "^8.0.1", + "co": "^4.6.0", + "collect-v8-coverage": "^1.0.2", + "color-convert": "^2.0.1", + "color-name": "^1.1.4", + "combined-stream": "^1.0.8", + "concat-map": "^0.0.1", + "convert-source-map": "^2.0.0", + "create-jest": "^29.7.0", + "cross-spawn": "^7.0.3", + "css.escape": "^1.5.1", + "cssom": "^0.5.0", + "cssstyle": "^2.3.0", + "data-urls": "^3.0.2", + "debug": "^4.3.4", + "decimal.js": "^10.4.3", + "dedent": "^1.5.1", + "deep-equal": "^2.2.2", + "deepmerge": "^4.3.1", + "define-data-property": "^1.1.0", + "define-properties": "^1.2.1", + "delayed-stream": "^1.0.0", + "detect-newline": "^3.1.0", + "diff-sequences": "^29.6.3", + "dom-accessibility-api": "^0.5.16", + "domexception": "^4.0.0", + "electron-to-chromium": "^1.4.551", + "emittery": "^0.13.1", + "emoji-regex": "^8.0.0", + "entities": "^4.5.0", + "error-ex": "^1.3.2", + "es-get-iterator": "^1.1.3", + "escalade": "^3.1.1", + "escape-string-regexp": "^2.0.0", + "escodegen": "^2.1.0", + "esprima": "^4.0.1", + "estraverse": "^5.3.0", + "esutils": "^2.0.3", + "execa": "^5.1.1", + "exit": "^0.1.2", + "expect": "^29.7.0", + "fast-json-stable-stringify": "^2.1.0", + "fb-watchman": "^2.0.2", + "fill-range": "^7.0.1", + "find-up": "^4.1.0", + "for-each": "^0.3.3", + "form-data": "^4.0.0", + "fs.realpath": "^1.0.0", + "function-bind": "^1.1.1", + "functions-have-names": "^1.2.3", + "gensync": "^1.0.0-beta.2", + "get-caller-file": "^2.0.5", + "get-intrinsic": "^1.2.1", + "get-package-type": "^0.1.0", + "get-stream": "^6.0.1", + "glob": "^7.2.3", + "globals": "^11.12.0", + "gopd": "^1.0.1", + "graceful-fs": "^4.2.11", + "has": "^1.0.4", + "has-bigints": "^1.0.2", + "has-flag": "^4.0.0", + "has-property-descriptors": "^1.0.0", + "has-proto": "^1.0.1", + "has-symbols": "^1.0.3", + "has-tostringtag": "^1.0.0", + "html-encoding-sniffer": "^3.0.0", + "html-escaper": "^2.0.2", + "http-proxy-agent": "^5.0.0", + "https-proxy-agent": "^5.0.1", + "human-signals": "^2.1.0", + "iconv-lite": "^0.6.3", + "import-local": "^3.1.0", + "imurmurhash": "^0.1.4", + "indent-string": "^4.0.0", + "inflight": "^1.0.6", + "inherits": "^2.0.4", + "internal-slot": "^1.0.5", + "is-arguments": "^1.1.1", + "is-array-buffer": "^3.0.2", + "is-arrayish": "^0.2.1", + "is-bigint": "^1.0.4", + "is-boolean-object": "^1.1.2", + "is-callable": "^1.2.7", + "is-core-module": "^2.13.0", + "is-date-object": "^1.0.5", + "is-fullwidth-code-point": "^3.0.0", + "is-generator-fn": "^2.1.0", + "is-map": "^2.0.2", + "is-number": "^7.0.0", + "is-number-object": "^1.0.7", + "is-potential-custom-element-name": "^1.0.1", + "is-regex": "^1.1.4", + "is-set": "^2.0.2", + "is-shared-array-buffer": "^1.0.2", + "is-stream": "^2.0.1", + "is-string": "^1.0.7", + "is-symbol": "^1.0.4", + "is-typed-array": "^1.1.12", + "is-weakmap": "^2.0.1", + "is-weakset": "^2.0.2", + "isarray": "^2.0.5", + "isexe": "^2.0.0", + "istanbul-lib-coverage": "^3.2.0", + "istanbul-lib-instrument": "^6.0.1", + "istanbul-lib-report": "^3.0.1", + "istanbul-lib-source-maps": "^4.0.1", + "istanbul-reports": "^3.1.6", + "jest-changed-files": "^29.7.0", + "jest-circus": "^29.7.0", + "jest-cli": "^29.7.0", + "jest-config": "^29.7.0", + "jest-diff": "^29.7.0", + "jest-docblock": "^29.7.0", + "jest-each": "^29.7.0", + "jest-environment-node": "^29.7.0", + "jest-get-type": "^29.6.3", + "jest-haste-map": "^29.7.0", + "jest-leak-detector": "^29.7.0", + "jest-matcher-utils": "^29.7.0", + "jest-message-util": "^29.7.0", + "jest-mock": "^29.7.0", + "jest-pnp-resolver": "^1.2.3", + "jest-regex-util": "^29.6.3", + "jest-resolve": "^29.7.0", + "jest-resolve-dependencies": "^29.7.0", + "jest-runner": "^29.7.0", + "jest-runtime": "^29.7.0", + "jest-snapshot": "^29.7.0", + "jest-util": "^29.7.0", + "jest-validate": "^29.7.0", + "jest-watcher": "^29.7.0", + "jest-worker": "^29.7.0", + "js-tokens": "^4.0.0", + "js-yaml": "^3.14.1", + "jsdom": "^20.0.3", + "jsesc": "^2.5.2", + "json-parse-even-better-errors": "^2.3.1", + "json5": "^2.2.3", + "kleur": "^3.0.3", + "leven": "^3.1.0", + "lines-and-columns": "^1.2.4", + "locate-path": "^5.0.0", + "lodash": "^4.17.21", + "lru-cache": "^5.1.1", + "lz-string": "^1.5.0", + "make-dir": "^4.0.0", + "makeerror": "^1.0.12", + "merge-stream": "^2.0.0", + "micromatch": "^4.0.5", + "mime-db": "^1.52.0", + "mime-types": "^2.1.35", + "mimic-fn": "^2.1.0", + "min-indent": "^1.0.1", + "minimatch": "^3.1.2", + "ms": "^2.1.2", + "natural-compare": "^1.4.0", + "node-int64": "^0.4.0", + "node-releases": "^2.0.13", + "normalize-path": "^3.0.0", + "npm-run-path": "^4.0.1", + "nwsapi": "^2.2.7", + "object-inspect": "^1.12.3", + "object-is": "^1.1.5", + "object-keys": "^1.1.1", + "object.assign": "^4.1.4", + "once": "^1.4.0", + "onetime": "^5.1.2", + "p-limit": "^3.1.0", + "p-locate": "^4.1.0", + "p-try": "^2.2.0", + "parse-json": "^5.2.0", + "parse5": "^7.1.2", + "path-exists": "^4.0.0", + "path-is-absolute": "^1.0.1", + "path-key": "^3.1.1", + "path-parse": "^1.0.7", + "picocolors": "^1.0.0", + "picomatch": "^2.3.1", + "pirates": "^4.0.6", + "pkg-dir": "^4.2.0", + "pretty-format": "^27.5.1", + "prompts": "^2.4.2", + "psl": "^1.9.0", + "punycode": "^2.3.0", + "pure-rand": "^6.0.4", + "querystringify": "^2.2.0", + "react-is": "^17.0.2", + "redent": "^3.0.0", + "regenerator-runtime": "^0.14.0", + "regexp.prototype.flags": "^1.5.1", + "require-directory": "^2.1.1", + "requires-port": "^1.0.0", + "resolve": "^1.22.8", + "resolve-cwd": "^3.0.0", + "resolve-from": "^5.0.0", + "resolve.exports": "^2.0.2", + "safer-buffer": "^2.1.2", + "saxes": "^6.0.0", + "semver": "^6.3.1", + "set-function-name": "^2.0.1", + "shebang-command": "^2.0.0", + "shebang-regex": "^3.0.0", + "side-channel": "^1.0.4", + "signal-exit": "^3.0.7", + "sisteransi": "^1.0.5", + "slash": "^3.0.0", + "source-map": "^0.6.1", + "source-map-support": "^0.5.13", + "sprintf-js": "^1.0.3", + "stack-utils": "^2.0.6", + "stop-iteration-iterator": "^1.0.0", + "string-length": "^4.0.2", + "string-width": "^4.2.3", + "strip-ansi": "^6.0.1", + "strip-bom": "^4.0.0", + "strip-final-newline": "^2.0.0", + "strip-indent": "^3.0.0", + "strip-json-comments": "^3.1.1", + "supports-color": "^7.2.0", + "supports-preserve-symlinks-flag": "^1.0.0", + "symbol-tree": "^3.2.4", + "test-exclude": "^6.0.0", + "tmpl": "^1.0.5", + "to-fast-properties": "^2.0.0", + "to-regex-range": "^5.0.1", + "tough-cookie": "^4.1.3", + "tr46": "^3.0.0", + "type-detect": "^4.0.8", + "type-fest": "^0.21.3", + "undici-types": "^5.25.3", + "universalify": "^0.2.0", + "update-browserslist-db": "^1.0.13", + "url-parse": "^1.5.10", + "v8-to-istanbul": "^9.1.3", + "w3c-xmlserializer": "^4.0.0", + "walker": "^1.0.8", + "webidl-conversions": "^7.0.0", + "whatwg-encoding": "^2.0.0", + "whatwg-mimetype": "^3.0.0", + "whatwg-url": "^11.0.0", + "which": "^2.0.2", + "which-boxed-primitive": "^1.0.2", + "which-collection": "^1.0.1", + "which-typed-array": "^1.1.11", + "wrap-ansi": "^7.0.0", + "wrappy": "^1.0.2", + "write-file-atomic": "^4.0.2", + "ws": "^8.14.2", + "xml-name-validator": "^4.0.0", + "xmlchars": "^2.2.0", + "y18n": "^5.0.8", + "yallist": "^3.1.1", + "yargs": "^17.7.2", + "yargs-parser": "^21.1.1", + "yocto-queue": "^0.1.0" + }, + "keywords": [], + "author": "" } diff --git a/week-2/debug/address.js b/week-2/debug/address.js index 940a6af8..0e7a8c1b 100644 --- a/week-2/debug/address.js +++ b/week-2/debug/address.js @@ -1,4 +1,5 @@ // Predict and explain first... +//It won't print because the code is an object and indexing doesn't work for an object unlike an array. // This code should log out the houseNumber from the address object // but it isn't working... @@ -9,7 +10,7 @@ const address = { street: "Imaginary Road", city: "Manchester", country: "England", - postcode: "XYZ 123", + postcode: "XYZ 123" }; -console.log(`My house number is ${address[0]}`); +console.log(`My house number is ${address.houseNumber}.`); diff --git a/week-2/debug/author.js b/week-2/debug/author.js index 8c212597..24aa8919 100644 --- a/week-2/debug/author.js +++ b/week-2/debug/author.js @@ -1,4 +1,5 @@ // Predict and explain first... +//Answer: The console would not print because "of" can't iterate through in for of "in" will and the dot won't work as well except if square brackets is used. // This program attempts to log out all the property values in the object. // But it isn't working. Explain why first and then fix the problem @@ -6,11 +7,10 @@ const author = { firstName: "Zadie", lastName: "Smith", - occupation: "writer", + occupation: "Writer", age: 40, alive: true, }; - -for (const value of author) { - console.log(value); +for (const value in author) { + console.log(author[value]); } diff --git a/week-2/debug/recipe.js b/week-2/debug/recipe.js index 6cbdd22c..efb158b7 100644 --- a/week-2/debug/recipe.js +++ b/week-2/debug/recipe.js @@ -1,4 +1,5 @@ // Predict and explain first... +//The ingredients will not print because it sees it as an object // This program should log out the title, how many it serves and the ingredients. // Each ingredient should be logged on a new line @@ -11,5 +12,8 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +ingredients: +${recipe.ingredients[0]} +${recipe.ingredients[1]} +${recipe.ingredients[2]} +${recipe.ingredients[3]}`); \ No newline at end of file diff --git a/week-2/implement/contains.js b/week-2/implement/contains.js index cd779308..576a09a4 100644 --- a/week-2/implement/contains.js +++ b/week-2/implement/contains.js @@ -1,3 +1,14 @@ -function contains() {} +function contains(obj,char) { + return (Object.keys(obj).includes(char)) ? true:false; +}; -module.exports = contains; +const myNum = { + a: 1, + b: 2, + c: 3, + d: 4, + e: 5, +}; +console.log(contains(myNum, "")); + +module.exports = contains; \ No newline at end of file diff --git a/week-2/implement/contains.test.js b/week-2/implement/contains.test.js index e75984b8..0db5e072 100644 --- a/week-2/implement/contains.test.js +++ b/week-2/implement/contains.test.js @@ -32,3 +32,23 @@ as the object doesn't contains a key of 'c' // Given invalid parameters like arrays // When passed to contains // Then it should return false or throw an error + +test("given an empty object, it should return false", () => { + const myNum = { }; + expect(contains(myNum, "")).toBe(false); +}); + +test("given an object with properties, it should return true", () => { + const myNum = { a: 1, b: 2, c: 3, d: 4, e: 5 }; + expect(contains(myNum, "b")).toBe(true); +}); + +test("given an object with a non-existent property name, it should return false", () => { + const myNum = { a: 1, b: 2, c: 3, d: 4, e: 5 }; + expect(contains(myNum, "z")).toBe(false); +}); + +test("given invalid parameters like arrays, it should return false or throw an error", () => { + const myNum = ["a: 1", "b: 2", "c: 3", "d: 4", "e : 5"]; + expect(contains(myNum, "c")).toBe(false); +}); \ No newline at end of file diff --git a/week-2/implement/lookup.js b/week-2/implement/lookup.js index d4677714..65508c25 100644 --- a/week-2/implement/lookup.js +++ b/week-2/implement/lookup.js @@ -1,17 +1,30 @@ -function createLookup() { +function createLookup(countryCurrencyPairs) { // implementation here + let myCurrency = {}; + for (const pair of countryCurrencyPairs ) { + const [countryCode, currencyCode] = pair; + myCurrency[countryCode] = currencyCode; + } + return myCurrency; } +const result = createLookup([ + ["GB", "GBP"], + ["DE", "EUR"], +]); +console.log(result); /* ======= Test suite is provided below... ===== */ -test("converts a single pair of currency codes", () => { - expect(createLookup([["GB", "GBP"]])).toEqual({ - GB: "GBP", - }); - expect(createLookup([["DE", "EUR"]])).toEqual({ - DE: "EUR", - }); -}); +// test("converts a single pair of currency codes", () => { +// expect(createLookup([["GB", "GBP"]])).toEqual({ +// GB: "GBP", +// }); +// expect(createLookup([["DE", "EUR"]])).toEqual({ +// DE: "EUR", +// }); +// }); -test.todo("creates a country currency code lookup for multiple codes"); +// test.todo("creates a country currency code lookup for multiple codes"); + +module.exports = createLookup; \ No newline at end of file diff --git a/week-2/implement/lookup.test.js b/week-2/implement/lookup.test.js index 8804cb65..b470570d 100644 --- a/week-2/implement/lookup.test.js +++ b/week-2/implement/lookup.test.js @@ -1,3 +1,4 @@ +const createLookup = require("./lookup.js"); /* Create a lookup object of key value pairs from an array of code pairs @@ -29,3 +30,35 @@ It should return: 'CA': 'CAD' } */ +// test("converts a single pair of currency codes", () => { +// const result = createLookup([ +// ["GB", "GBP"], +// ["DE", "EUR"], +// ]); +// expect(createLookup([["GB", "GBP"]])).toEqual({ +// "GB": "GBP", "CA":"CAD" +// }); +// expect(createLookup([["DE", "EUR"]])).toEqual({ +// DE: "EUR", +// }); +// }); + +test("converts a single pair of currency codes", () => { + expect(createLookup([["GB", "GBP"]])).toEqual({ + GB: "GBP", + }); + expect(createLookup([["DE", "EUR"]])).toEqual({ + DE: "EUR", + }); +}); + +test("creates a country currency code lookup for multiple codes", () =>{ + const result = createLookup([ + ["GB", "GBP"], + ["DE", "EUR"], + ]); + expect(result).toEqual({ + GB: "GBP", + DE: "EUR", + }); +}); diff --git a/week-2/implement/tally.js b/week-2/implement/tally.js index e69de29b..59795107 100644 --- a/week-2/implement/tally.js +++ b/week-2/implement/tally.js @@ -0,0 +1,21 @@ +function tally(arr) { + if (typeof arr != "object") { + return "It is an error!"; + } + let myTallyCount = {}; + + for (item of arr) { + if (myTallyCount[item]) { + myTallyCount[item] += 1; + } else { + myTallyCount[item] = 1; + } + } + return myTallyCount; +} +const myCount = "a"; +const myCount1 = ["a1", "b", "c2", "a1", "r", "c2", "c2"]; +console.log(tally(myCount)); +console.log(tally(myCount1)); + +module.exports = tally; diff --git a/week-2/implement/tally.test.js b/week-2/implement/tally.test.js index b473b750..ef06c34d 100644 --- a/week-2/implement/tally.test.js +++ b/week-2/implement/tally.test.js @@ -1,3 +1,4 @@ +const tally = require("./tally.js"); /** * tally array * @@ -29,3 +30,23 @@ // Given an invalid input like a string // When passed to tally // Then it should throw an error + +test("given a function called tally, should return an object containing the count for each unique item", () => { + const myCount = ["a", "a", "a"]; + expect(tally(myCount)).toEqual({a: 3}); +}); + +test("given an empty array, should return an empty object", () => { + const myCount = []; + expect(tally(myCount)).toEqual({}); +}); + +test("given an array with duplicate item, should return count for each unique item", () => { + const myCount = ["a", "a", "a", 2, "p", "I", "I", 2]; + expect(tally(myCount)).toEqual({ 2: 2, a: 3, p: 1, I: 2 }); +}); + +test("given an invalid input like a string, should throw an error", () => { + const myCount = "a"; + expect(tally(myCount)).toEqual("It is an error!"); +}); \ No newline at end of file diff --git a/week-2/interpret/invert.js b/week-2/interpret/invert.js index 00d27025..5e391f30 100644 --- a/week-2/interpret/invert.js +++ b/week-2/interpret/invert.js @@ -10,18 +10,25 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + invertedObj[value] = key; } return invertedObj; } - +console.log(invert({a:1, b:2})); // a) What is the current return value when invert is called with { a : 1 } +//The current return value when invert is called with {a:1} is { key: 1 }. // b) What is the current return value when invert is called with { a: 1, b: 2 } +//The current return value when invert is called with { a: 1, b: 2 } is { key: 2 }. // c) What is the target return value when invert is called with {a : 1, b: 2} +//The target return value when invert is called with {a : 1, b: 2} is {1 : a, 2 : b}. // c) What does Object.entries return? Why is it needed in this program? - +/*Object.entries returns an array of a given object own enumerable property [key, value] pairs. +(2) It is needed in the program because it allows the iteration of properties of the object[key, value] pair if the for of loop // d) Explain why the current return value is different from the target output +/*The current value is different from the target value because in the loop, +invertedObj.key=value is overwriting the property named in each iteration, +not using the actual key from the original object. This can be corrected by using square bracket invertedObj[value]=key.*/ \ No newline at end of file diff --git a/week-2/package.json b/week-2/package.json index 3904f6d0..18060c01 100644 --- a/week-2/package.json +++ b/week-2/package.json @@ -2,7 +2,7 @@ "name": "week-2", "version": "1.0.0", "description": "This README will guide you through the different sections for this week.", - "main": "", + "main": "index.js", "scripts": { "test": "jest" }, diff --git a/week-2/stretch/count-words.js b/week-2/stretch/count-words.js index 43c04c4e..4c74af4f 100644 --- a/week-2/stretch/count-words.js +++ b/week-2/stretch/count-words.js @@ -26,3 +26,24 @@ 3. Order the results to find out which word is the most common in the chapter */ + +function countWords (myString){ + const removeSymbol = myString.replaceAll(/[^\w\s]/g, "").toLowerCase(); + const strToArray = removeSymbol.split(" "); + const myStrObject = {}; + + for(const item of strToArray){ + if(myStrObject[item]){ + myStrObject[item] += 1; + }else{ + myStrObject[item] = 1; + } + } + const objectToArray = Object.entries(myStrObject); + const sortedArray = objectToArray.sort((a, b) => b[1] - a[1]); + const sortedObject = Object.fromEntries(sortedArray); + + return sortedObject; +} +let stringToObject = countWords("You? and, me! and= you and."); +console.log(stringToObject); \ No newline at end of file diff --git a/week-2/stretch/mode.js b/week-2/stretch/mode.js index a3e2e2e0..39edf737 100644 --- a/week-2/stretch/mode.js +++ b/week-2/stretch/mode.js @@ -8,8 +8,15 @@ // refactor calculateMode by splitting up the code // into smaller functions using the stages above + function calculateMode(list) { - // track frequency of each value + trackFrequency(list); + const mode = valueHighestFrequency(list); + + return mode; +} + +function trackFrequency(list) { let freqs = new Map(); for (let num of list) { @@ -17,18 +24,24 @@ function calculateMode(list) { freqs.set(num, (freqs.get(num) || 0) + 1); } + return freqs; +} - // Find the value with the highest frequency +function valueHighestFrequency(list) { let maxFreq = 0; let mode; + let freqs = trackFrequency(list) + for (let [num, freq] of freqs) { if (freq > maxFreq) { mode = num; maxFreq = freq; } } - return maxFreq === 0 ? NaN : mode; } -module.exports = calculateMode; +let result = calculateMode([1, 4, 1, 2, 3, 2, 1]); +console.log(result); + +module.exports = calculateMode; \ No newline at end of file diff --git a/week-2/stretch/till.js b/week-2/stretch/till.js index e3207f94..5245db84 100644 --- a/week-2/stretch/till.js +++ b/week-2/stretch/till.js @@ -8,10 +8,8 @@ function totalTill(till) { let total = 0; for (const [coin, quantity] of Object.entries(till)) { - console.log(coin * quantity); - total += coin * quantity; + total += parseFloat(coin) * quantity; } - return `£${total / 100}`; } @@ -22,11 +20,21 @@ const till = { "20p": 10, }; const totalAmount = totalTill(till); +console.log(totalAmount); + +const emptytill = {}; +const tillAmount = totalTill(emptytill); +console.log(tillAmount); // a) What is the target output when totalTill is called with the till object +//The target output is £4.4. // b) Why do we need to use Object.entries inside the for...of loop in this function? +// we need Object.entries to change the object to an array // c) What does coin * quantity evaluate to inside the for...of loop? +//It evaluates to NaN. // d) Write a test for this function to check it works and then fix the implementation of totalTill + +module.exports = totalTill; \ No newline at end of file