Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function horse_racing(data) {
- let horses = data.shift().split("|");
- data.pop()
- for (const line of data) {
- let command_data = line.split(' ')
- let command = command_data[0]
- if (command === "Retake") {
- let first_horse = command_data[1]
- let second_horse = command_data[2]
- if (horses.includes(first_horse) && horses.includes(second_horse)) {
- let first_index = horses.indexOf(first_horse);
- let second_index = horses.indexOf(second_horse);
- if (first_index < second_index) {
- let overtaking = horses[first_index];
- horses[first_index] = horses[second_index];
- horses[second_index] = overtaking;
- console.log(`${first_horse} retakes ${second_horse}.`);
- }
- }
- } else if (command === "Trouble") {
- let first_horse = command_data[1]
- if (horses.includes(first_horse)) {
- let horse_index = horses.indexOf(first_horse);
- if (horse_index > 0) {
- let deleted_horse = horses.splice(horse_index, 1)[0];
- horses.splice(horse_index - 1, 0, deleted_horse);
- console.log(`Trouble for ${first_horse} - drops one position.`);
- }
- }
- } else if (command === "Rage") {
- let firs_horse = command_data[1]
- let index = horses.indexOf(firs_horse);
- if (index === horses.length - 1) {
- console.log(`${firs_horse} rages 2 positions ahead.`);
- }
- else if (index === horses.length - 2) {
- let deleted_horse = horses.splice(index, 1)[0];
- horses.splice(index + 1, 0, deleted_horse);
- console.log(`${firs_horse} rages 2 positions ahead.`);
- }
- else if (index < horses.length - 2) {
- let deleted_horse = horses.splice(index, 1)[0];
- horses.splice(index + 2, 0, deleted_horse);
- console.log(`${firs_horse} rages 2 positions ahead.`);
- }
- } else if (command === "Miracle") {
- let deleted_horse = horses.splice(0, 1)[0];
- horses.splice(horses.length, 0, deleted_horse);
- console.log(`What a miracle - ${deleted_horse} becomes first.`);
- }
- }
- console.log(horses.join('->'));
- console.log(`The winner is: ${horses[horses.length - 1]}`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement