curriculum/challenges/english/blocks/daily-coding-challenges-javascript/68cae5b538ff798bbd4da001.md
Given two sentences representing your team and an opposing team, where each word from your team battles the corresponding word from the opposing team, determine which team wins using the following rules:
a to z correspond to the values 1 through 26. For example, a is 1, and z is 26.A is 2, and Z is 52.Return "We win" if your team is the winner, "We lose" if your team loses, and "Draw" if both teams have the same number of wins.
battle("hello world", "hello word") should return "We win".
assert.equal(battle("hello world", "hello word"), "We win");
battle("Hello world", "hello world") should return "We win".
assert.equal(battle("Hello world", "hello world"), "We win");
battle("lorem ipsum", "kitty ipsum") should return "We lose".
assert.equal(battle("lorem ipsum", "kitty ipsum"), "We lose");
battle("hello world", "world hello") should return "Draw".
assert.equal(battle("hello world", "world hello"), "Draw");
battle("git checkout", "git switch") should return "We win".
assert.equal(battle("git checkout", "git switch"), "We win");
battle("Cheeseburger with fries", "Cheeseburger with Fries") should return "We lose".
assert.equal(battle("Cheeseburger with fries", "Cheeseburger with Fries"), "We lose");
battle("We must never surrender", "Our team must win") should return "Draw".
assert.equal(battle("We must never surrender", "Our team must win"), "Draw");
function battle(ourTeam, opponent) {
return ourTeam;
}
function getCharacterValue(char) {
const lowerCaseValue = char.toLowerCase().charCodeAt(0) - 96;
return char >= 'A' && char <= 'Z' ? lowerCaseValue * 2 : lowerCaseValue;
}
function getWordValue(word) {
let wordValue = 0;
for (let i=0; i<word.length; i++) {
wordValue += getCharacterValue(word[i])
}
return wordValue;
}
function battle(ourTeam, opponent) {
const myWords = ourTeam.split(' ');
const opponentWords = opponent.split(' ');
let myWins = 0, opponentWins = 0;
for (let i=0; i<myWords.length; i++) {
const myWordValue = getWordValue(myWords[i]);
const opponentWordValue = getWordValue(opponentWords[i])
if (myWordValue > opponentWordValue) myWins++;
if (opponentWordValue > myWordValue) opponentWins++;
}
if (myWins > opponentWins) return 'We win';
if (opponentWins > myWins) return 'We lose';
return 'Draw';
}