Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function darts(input) {
- let player = input[0];
- let index = 1;
- let points = 301;
- let command = input[index];
- let shot = Number(input[index + 1]);
- let successfullShots = 0;
- let unsuccessfulShots = 0;
- while((command !== 'Retire') && points !== 0) {
- if(command === 'Single') {
- if(points >= shot) {
- points -= shot;
- successfullShots++;
- } else {
- unsuccessfulShots++;
- }
- } else if(command === 'Double') {
- if(points >= shot) {
- points -= shot * 2;
- successfullShots++;
- } else {
- unsuccessfulShots++;
- }
- } else if(command === 'Triple') {
- if(points >= shot) {
- points -= shot * 3;
- successfullShots++;
- } else {
- unsuccessfulShots++;
- }
- } else if(command === 'Retire') {
- break;
- }
- index += 2;
- command = input[index];
- shot = Number(input[index + 1]);
- }
- if(points = 0) {
- console.log(`${player} won the leg with ${successfullShots} shots.`);
- } else if(command === 'Retire') {
- console.log(`${player} retired after ${unsuccessfulShots} unsuccessful shots.`);
- }
- }
Add Comment
Please, Sign In to add comment