Created
June 12, 2020 02:40
-
-
Save sandrabosk/5f13b63692737a57bc59c4655d20f37b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ************************************************************************************ | |
// https://www.codewars.com/kata/good-vs-evil | |
// Description | |
// Middle Earth is about to go to war. The forces of good will have many battles with | |
// the forces of evil. Different races will certainly be involved. Each race has a | |
// certain worth when battling against others. On the side of good we have the following | |
// races, with their associated worth: | |
// Hobbits: 1 | |
// Men: 2 | |
// Elves: 3 | |
// Dwarves: 3 | |
// Eagles: 4 | |
// Wizards: 10 | |
// On the side of evil we have: | |
// Orcs: 1 | |
// Men: 2 | |
// Wargs: 2 | |
// Goblins: 2 | |
// Uruk Hai: 3 | |
// Trolls: 5 | |
// Wizards: 10 | |
// Although weather, location, supplies and valor play a part in any battle, if you add | |
// up the worth of the side of good and compare it with the worth of the side of evil, | |
// the side with the larger worth will tend to win. | |
// Thus, given the count of each of the races on the side of good, followed by the count | |
// of each of the races on the side of evil, determine which side wins. | |
// Input: | |
// The function will be given two parameters. Each parameter will be a string of multiple | |
// integers separated by a single space. Each string will contain the count of each race | |
// on the side of good and evil. | |
// The first parameter will contain the count of each race on the side of good in the following order: | |
// Hobbits, Men, Elves, Dwarves, Eagles, Wizards. | |
// The second parameter will contain the count of each race on the side of evil in the following order: | |
// Orcs, Men, Wargs, Goblins, Uruk Hai, Trolls, Wizards. | |
// All values are non-negative integers. The resulting sum of the worth for each side will not exceed | |
// the limit of a 32-bit integer. | |
// Output: | |
// Return "Battle Result: Good triumphs over Evil" if good wins, | |
// "Battle Result: Evil eradicates all trace of Good" if evil wins, or | |
// "Battle Result: No victor on this battle field" if it ends in a tie. | |
// ************************************************************************************ | |
const goodWorth = [ | |
1, // Hobbits | |
2, // Men | |
3, // Elves | |
3, // Dwarves | |
4, // Eagles | |
10, // Wizards | |
] | |
const evilWorth = [ | |
1, // Orcs | |
2, // Men | |
2, // Wargs | |
2, // Goblins | |
3, // Uruk Hai | |
5, // Trolls | |
10, // Wizards | |
] | |
const calculateWorth = (points, force) => | |
points | |
.split(' ') | |
.reduce( | |
(total, value, index) => | |
force === 'good' | |
? Number(total) + goodWorth[index] * Number(value) | |
: Number(total) + evilWorth[index] * Number(value), | |
0 | |
) | |
function goodVsEvil(good, evil) { | |
const goodTotalWorth = calculateWorth(good, 'good') | |
const evilTotalWorth = calculateWorth(evil, 'evil') | |
if (evilTotalWorth > goodTotalWorth) | |
return 'Battle Result: Evil eradicates all trace of Good' | |
else if (goodTotalWorth > evilTotalWorth) | |
return 'Battle Result: Good triumphs over Evil' | |
else return 'Battle Result: No victor on this battle field' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
`function goodVsEvil(good, evil){
let goodArr = good.split(' ').map(Number),
evilArr = evil.split(' ').map(Number),
countGood = 0,
countEvil = 0;
for(let i = 0; i <= goodArr.length; i++){
if(goodArr[i] > 0) countGood += goodArr[i];
}
for(let i = 0; i <= evilArr.length; i++){
if(evilArr[i] > 0) countEvil += evilArr[i];
}
if(countGood > countEvil) return console.log('Battle Result: Good triumphs over Evil');
if(countGood < countEvil) return console.log('Battle Result: Evil eradicates all trace of Good');
if(countGood == countEvil) return console.log('Battle Results: No victor on this battle field');
}`