Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function words(input) {
- let [words, ...comands] = input;
- words = words.split(' ');
- for (let current of comands) {
- let [comand, arg1, arg2] = current.split(' ');
- if (comand == 'Delete') {
- arg1 = Number(arg1);
- if (arg1 <= (words.length - 2) && arg1 >= 0) {
- let index = arg1 + 1;
- words.splice(index, 1);
- }
- } else if (comand == 'Swap') {
- let word1 = arg1;
- let word2 = arg2;
- if (words.includes(word1) && words.includes(word2)) {
- let index1 = words.indexOf(word1);
- let index2 = words.indexOf(word2);
- words.splice(index1, 1, word2);
- words.splice(index2, 1, word1);
- }
- } else if (comand == 'Put') {
- let index = Number(arg2);
- let word = arg1;
- if (index > 0 && index < (words.length - 1)) {
- words.splice(index - 1, 0, word);
- } else if (index == (words.length - 1)) {
- words.push(word);
- }
- } else if (comand == 'Sort') {
- words = words.sort((a, b) => b.localeCompare(a));
- } else if (comand == 'Replace') {
- let word1 = arg1;
- let word2 = arg2;
- if (words.includes(word2)) {
- let index = words.indexOf(word2);
- words.splice(index, 1, word1);
- }
- } else if (comand == 'Stop') {
- console.log(words.join(' '));
- }
- }
- }
- words(["Congratulations! You last also through the have challenge!",
- "Swap have last",
- "Replace made have",
- "Delete 2",
- "Put it 4",
- "Stop"
- ]);
- words(["This the my quest! final","Put is 2","Swap final quest!",
- "Delete 2",
- "Stop"]);
- /*2 crit sorting*/
- /*words = words.sort((a, b) => b.length - a.length || b.localeCompare(a)); */
- /*3. Problem
- Create a program that follows given instructions. You will receive a collection of words on a single line, split by a single space. They are not what they are supposed to be, so you have to follow the instructions in order to find the real message. you will be receiving commands. Here are the possible ones: Delete {index}— removes the word after the given index if It is valid. Swap {word* {word2}— find the given words in the collections if tlist and swap their places. Put {word} index) — add a word at the previous place (index) before the given one If It is valid. Note: If the given index is the and of the list putting the word to the and of the list. If given Index Is out of range skip the command. Sort — you must sort the words In descending order. Replace {word* {word2} — find the second word (word2} in the collection (fjf,gyiatt and replace it with the first word — (worth).
- Follow them until you receive the "Stop" command. After you have successfully followed the instructions, you must print the words on a single line, split by a space.
- Input / Constraints •
- On the I. line, you are going to receive the collection of words, split by a single space • On the next lines, you are going to receive commands, until you receive the "Stop" command
- Output • Print the words you have gathered on a single line, split by a single space Examples
- Input
- Output
- */
Add Comment
Please, Sign In to add comment