Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arr){
- let weaponName = arr.shift().split('|');
- let el = '';
- for(let element of arr){
- if(element === 'Done'){
- break;
- }
- let allCommand = element.split(' ');// Move Right 0
- let command = allCommand.shift();// Move
- let position = allCommand.shift();// Right
- switch(command){
- case 'Move':
- let index = Number(allCommand);
- switch(position){
- case 'Right':
- if(index < 0 || index >= weaponName.length){
- break;
- }else if(index >= 0 && index < weaponName.length - 1){
- [weaponName[index + 1], weaponName[index]] = [weaponName[index], weaponName[index + 1]];
- }
- ;break;
- case 'Left':
- if(index < 0 || index >= weaponName.length){
- break;
- }else if(index > 0 && index < weaponName.length){
- [weaponName[index - 1], weaponName[index]] = [weaponName[index], weaponName[index - 1]]
- }
- ;break;
- };break;
- case 'Check': switch(position){
- case 'Even':
- console.log(weaponName.filter((el,index)=> index % 2 === 0).join(' '));
- ;break;
- case 'Odd':
- console.log(weaponName.filter((el, index) => index % 2 !== 0).join(' '));
- ;break;
- };break;
- }
- }
- console.log(weaponName.join(''))
- }
- solve([
- 'ha|Do|mm|om|er',
- 'Move Right 0',
- 'Move Left 3',
- 'Check Odd',
- 'Move Left 2',
- 'Move Left 10',
- 'Move Left 0',
- 'Done'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment