Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 89 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
"author": "Build Circle Ltd",
"license": "ISC",
"dependencies": {
"axios": "1.7.2",
"express": "^4.18.2"
},
"devDependencies": {
"supertest": "^6.3.4",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.18",
"eslint": "^8.56.0",
"jest": "^29.7.0"
"jest": "^29.7.0",
"nock": "13.5.4",
"supertest": "^6.3.4"
}
}
37 changes: 30 additions & 7 deletions src/battle.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
const getCharacters = require('./getCharacters')
const getCharacters = require("./getCharacters");

function battle(heroName, villainName) {
const characters = getCharacters()
async function battle(heroName, villainName) {
const characters = await getCharacters();

const hero = characters.items.find(e => e.name === heroName)
const villain = characters.items.find(e => e.name === villainName)
const hero = characters.items.find(e => e.name === heroName);
const villain = characters.items.find(e => e.name === villainName);

return hero.score >= villain.score ? hero : villain
if (!hero || !villain) {
throw new Error("failed to find characters");
}

let heroScore = hero.score;
let villainScore = villain.score;

if (hero.weakness === villain.name) {
// I decided to add score rather than subtract,
// as it will elliminate the need to check for negative scores
villainScore += 1;
}

if (villain.weakness === hero.name) {
heroScore += 1;
}

if (heroScore === villainScore) {
return null;
}

return heroScore >= villainScore
? { ...hero, score: heroScore }
: { ...villain, score: villainScore };
}

module.exports = battle
module.exports = battle;
Loading