Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let namePlayer1 = input[0];
- let namePlayer2 = input[1];
- let index = 2;
- let pointsP1 = 0;
- let pointsP2 = 0;
- let winner = 0;
- let nameOfWinner = '';
- while (input[index] !== 'End of game') {
- let p1Card = Number(input[index]);
- let p2Card = Number(input[index + 1]);
- if (p1Card > p2Card) {
- pointsP1 += p1Card - p2Card;
- } else if (p1Card < p2Card) {
- pointsP2 += p2Card - p1Card;
- } else {
- index = index + 2;
- p1Card = Number(input[index]);
- p2Card = Number(input[index + 1]);
- if (p1Card > p2Card) {
- pointsP1 = p1Card - p2Card;
- winner = pointsP1;
- nameOfWinner = namePlayer1;
- } else if (p1Card < p2Card) {
- pointsP2 = p2Card - p1Card;
- winner = pointsP2;
- nameOfWinner = namePlayer2;
- }
- console.log(`Number wars!`);
- console.log(`${nameOfWinner} is winner with ${winner} points`);
- break;
- }
- index = index + 2;
- }
- if (input[index] === 'End of game') {
- console.log(`${namePlayer1} has ${pointsP1} points`);
- console.log(`${namePlayer2} has ${pointsP2} points`);
- }
- }
- solve([
- // 'Desi',
- // 'Niki',
- // '7',
- // '5',
- // '3',
- // '4',
- // '3',
- // '3',
- // '5',
- // '3',
- 'Aleks',
- 'Georgi',
- '4',
- '5',
- '3',
- '2',
- '4',
- '3',
- '4',
- '4',
- '5',
- '2',
- // 'Elena',
- // 'Simeon',
- // '6',
- // '3',
- // '2',
- // '5',
- // '8',
- // '9',
- // 'End of game',
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement