Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function horse_racing(data) {
- let horse_positions = data.shift().split('|')
- data.pop()
- for (const line of data) {
- let command_data = line.split(' ')
- let command = command_data[0]
- if (command === 'Retake') {
- let overtaking_horse = command_data[1]
- let overtaken_horse = command_data[2]
- let overtaking_index = horse_positions.indexOf(overtaking_horse)
- let overtaken_index = horse_positions.indexOf(overtaken_horse);
- [horse_positions[overtaking_index], horse_positions[overtaken_index]] = [horse_positions[overtaken_index], horse_positions[overtaking_index]]
- console.log(`${overtaking_horse} retakes ${overtaken_horse}.`)
- }
- else if (command === 'Trouble') {
- let trouble_horse = command_data[1]
- if (horse_positions.indexOf(trouble_horse) !== 0) {
- let horse_index = horse_positions.indexOf(trouble_horse);
- [horse_positions[horse_index], horse_positions[horse_index + 1]] = [horse_positions[horse_index + 1], horse_positions[horse_index]]
- console.log(`Trouble for ${trouble_horse} - drops one position.`)
- }
- }
- else if (command === 'Rage') {
- let rage_horse = command_data[1]
- let horse_index = horse_positions.indexOf(rage_horse)
- if (horse_index === horse_positions[horse_positions.length - 2]) {
- let current_horse = horse_positions.splice(horse_index, 1)
- horse_positions.push(current_horse);
- } else if (horse_index < 1) {
- [horse_positions[horse_index], horse_positions[horse_index - 2]] = [horse_positions[horse_index - 2], horse_positions[horse_index]]
- }
- console.log(`${rage_horse} rages 2 positions ahead.`)
- }
- else if (command === 'Miracle') {
- let last_horse = horse_positions.shift()
- horse_positions.push(last_horse)
- console.log(`What a miracle - ${last_horse} becomes first.`)
- }
- }
- console.log(horse_positions.join('->'))
- console.log(`The winner is: ${horse_positions[horse_positions.length - 1]}`)
- }
- horse_racing(['Onyx|Domino|Sugar|Fiona',
- 'Trouble Onyx',
- 'Retake Onyx Sugar',
- 'Rage Domino',
- 'Miracle',
- 'Finish'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement