Skip to content

Commit

Permalink
Merge pull request #53 from glaucia86/GL/4-final-solution-exercise-no…
Browse files Browse the repository at this point in the history
…dejs-debug

Update and create final solutions for the 'debug Node.js apps' course
  • Loading branch information
glaucia86 authored Jan 9, 2024
2 parents 9d7c171 + c0aad22 commit 026edb9
Show file tree
Hide file tree
Showing 10 changed files with 3,276 additions and 12 deletions.
6 changes: 3 additions & 3 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ updates:
update-types: ["version-update:semver-patch"]

- package-ecosystem: "npm"
directory: "node-dependencies/5-exercise-dependency"
directory: "nodejs-dependencies"
schedule:
interval: "weekly"
open-pull-requests-limit: 10
Expand All @@ -19,14 +19,14 @@ updates:
update-types: ["version-update:semver-patch"]

- package-ecosystem: "npm"
directory: "nodejs-dependencies/7-exercise-dependency-management"
directory: "nodejs-debug"
schedule:
interval: "weekly"
open-pull-requests-limit: 10
ignore:
- dependency-name: "*"
update-types: ["version-update:semver-patch"]

- package-ecosystem: "npm"
directory: "nodejs-http/exercise-express-middleware"
schedule:
Expand Down
12 changes: 10 additions & 2 deletions .github/workflows/build-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ jobs:
npm install
npm run format
npm run lint
- name: Module 3 - node-dependencies
- name: Module 2 - node-dependencies
run: |
cd node-dependencies
npm install
npm run format
npm run lint
npm test
npm test
- name: Module 3 - nodejs-debug
run: |
cd node-dependencies
npm install
npm run format
npm run lint
npm test
10 changes: 10 additions & 0 deletions nodejs-debug/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = auto
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
quote_type = single
25 changes: 25 additions & 0 deletions nodejs-debug/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"env": {
"commonjs": true,
"es2021": true,
"node": true
},
"extends": "eslint:recommended",
"parser": "@babel/eslint-parser",
"parserOptions": {
"ecmaVersion": "latest",
"requireConfigFile": false,
"babelOptions": {
"parserOpts": {
"plugins": ["topLevelAwait"]
}
}
},
"rules": {
"no-console": "off",
"no-undef": "off",
"no-unused-vars": "off",
"no-shadow": "off",
"require-await": "error"
}
}
16 changes: 16 additions & 0 deletions nodejs-debug/4-exercise-built-debugger/fibonacci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function fibonacci(n) {
let n1 = 0;
let n2 = 1;
let sum = 0;

for (let i = 2; i < n; i++) {
sum = n1 + n2;
n1 = n2;
n2 = sum;
}

return n === 0 ? n1 : n2;
}

const result = fibonacci(5);
console.log(result);
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ function fibonacci(n) {
let n2 = 1;
let sum = 0;

const json = {
n1,
n2,
sum,
};

for (let i = 2; i <= n; i++) {
sum = n1 + n2;
n1 = n2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ function printForeignValues(value, sourceCurrency) {

setExchangeRate(0.88, 'USD', 'EUR');
setExchangeRate(107.4, 'USD', 'JPY');
printForeignValues(10, 'EUR');
printForeignValues(10, 'EUR');
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const rates = {};

function setExchangeRate(rate, sourceCurrency, targetCurrency) {
if (rates[sourceCurrency] === undefined) {
rates[sourceCurrency] = {};
}

if (rates[targetCurrency] === undefined) {
rates[targetCurrency] = {};
}

for (const currency in rates) {
if (currency !== targetCurrency) {
// Use a pivot rate for currencies that don't have the direct conversion rate
const pivotRate = currency === sourceCurrency ? 1 : rates[currency][sourceCurrency];
rates[currency][targetCurrency] = rate * pivotRate;
rates[targetCurrency][currency] = 1 / (rate * pivotRate);
}
}
}

function convertToCurrency(value, sourceCurrency, targetCurrency) {
const exchangeRate = rates[sourceCurrency][targetCurrency];
return exchangeRate && value * exchangeRate;
}

function formatValueForDisplay(value) {
return value.toFixed(2);
}

function printForeignValues(value, sourceCurrency) {
console.info(`The value of ${value} ${sourceCurrency} is:`);

for (const targetCurrency in rates) {
if (targetCurrency !== sourceCurrency) {
const convertedValue = convertToCurrency(value, sourceCurrency, targetCurrency);
const displayValue = formatValueForDisplay(convertedValue);
console.info(`- ${displayValue} ${targetCurrency}`);
}
}
}

setExchangeRate(0.88, 'USD', 'EUR');
setExchangeRate(107.4, 'USD', 'JPY');
printForeignValues(10, 'EUR');
Loading

0 comments on commit 026edb9

Please sign in to comment.