From ddb628d7485a54d3b011b81148087cd37e71df61 Mon Sep 17 00:00:00 2001 From: harjaus Date: Fri, 20 Sep 2019 17:10:09 +0100 Subject: [PATCH 1/2] Fixed inconsistency with test suite --- exercises/07_unit_testing/todo/modules/todo.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/07_unit_testing/todo/modules/todo.js b/exercises/07_unit_testing/todo/modules/todo.js index 5a034683..f4fdd7b8 100644 --- a/exercises/07_unit_testing/todo/modules/todo.js +++ b/exercises/07_unit_testing/todo/modules/todo.js @@ -9,7 +9,7 @@ module.exports.clear = () => { module.exports.add = (item, qty) => { qty = Number(qty) - if(isNaN(qty)) throw new Error('the quantity must be a number') + if(isNaN(qty)) throw new Error('qty must be a number') data.push({item: item, qty: qty}) } From 660bcf19ea719070573eec694809f78b285763ca Mon Sep 17 00:00:00 2001 From: harjaus Date: Fri, 20 Sep 2019 17:57:26 +0100 Subject: [PATCH 2/2] Added missing file for sheet 06. --- exercises/06_code_quality/nestedCallbacks.js | 61 ++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 exercises/06_code_quality/nestedCallbacks.js diff --git a/exercises/06_code_quality/nestedCallbacks.js b/exercises/06_code_quality/nestedCallbacks.js new file mode 100644 index 00000000..cd3fcc12 --- /dev/null +++ b/exercises/06_code_quality/nestedCallbacks.js @@ -0,0 +1,61 @@ + +'use strict' + +const request = require('request') + +getInput('enter base currency', (err, base) => { + if (err) { + console.log(err.message) + process.exit() + } + base = base.trim() + checkValidCurrencyCode(base, err => { + if (err) { + console.log(err.message) + process.exit() + } + getData(`http://api.fixer.io/latest?base=${base}`, (err, data) => { + if (err) { + console.log(err.message) + process.exit() + } + const obj = JSON.parse(data) + printObject(obj) + process.exit() + }) + }) +}) + +function getInput(prompt, callback) { + try { + process.stdin.resume() + process.stdin.setEncoding('utf8') + process.stdout.write(`${prompt}: `) + process.stdin.on('data', text => callback(null, text)) + } catch(err) { + callback(err) + } +} + +function checkValidCurrencyCode(code, callback) { + code = code.trim() + request('http://api.fixer.io/latest', (err, res, body) => { + if (err) callback(new Error('invalid API call')) + const rates = JSON.parse(body).rates + if (!rates.hasOwnProperty(code)) callback(new Error(`invalid currency code ${code}`)) + callback(null, true) + }) +} + +function getData(url, callback) { + request(url, (err, res, body) => { + if (err) callback(new Error('invalid API call')) + callback(null, body) + }) +} + +function printObject(data) { + const indent = 2 + const str = JSON.stringify(data, null, indent) + console.log(str) +}