Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function wars(input) {
- let playerOne = input[0];
- let playerTwo = input[1];
- let onePlScore = 0;
- let twoPlScore = 0
- for (let i = 2; i < input.length; i += 2) {
- if (input[i] === 'End of game') {
- console.log(`${playerOne} has ${onePlScore} points`)
- console.log(`${playerTwo} has ${twoPlScore} points`)
- return
- }
- let oneCard = Number(input[i])
- let twoCard = Number(input[i + 1])
- if (oneCard > twoCard) {
- onePlScore += oneCard - twoCard
- } else if (twoCard > oneCard) {
- twoPlScore += twoCard - oneCard
- } else {
- console.log("Number wars!");
- break;
- }
- }
- if (onePlScore > twoPlScore) {
- console.log(`${playerOne} is winner with ${onePlScore} points`)
- } else {
- console.log(`${playerTwo} is winner with ${twoPlScore} points`)
- }
- }
- wars([
- 'Desi',
- 'Niki',
- '7',
- '5',
- '3',
- '4',
- '3',
- '3',
- '5',
- '3'
- ])
- // wars(['Aleks',
- // 'Georgi',
- // '4',
- // '5',
- // '3',
- // '2',
- // '4',
- // '3',
- // '4',
- // '4',
- // '5',
- // '2'
- // ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement