Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function passwordReset(input) {
- let password = input.shift();
- let commandParser = {
- 'TakeOdd': (password) => {
- return [...password].filter((symbol, index) => {
- return index % 2 !== 0;
- }).join('');
- },
- 'Cut': (password, index, length) => {
- index = Number(index);
- length = Number(length);
- let substring = password.substr(index, length);
- return password.replace(substring, '');
- },
- 'Substitute': (password, substring, substitute) => {
- if (password.includes(substring)) {
- return password.replace(new RegExp(substring, 'g'), substitute);
- }
- console.log("Nothing to replace!");
- return password;
- }
- };
- input.forEach(line => {
- if (line !== 'Done') {
- let [command,...tokens] = line.split(' ');
- let oldPassword = password;
- password = commandParser[command](password,...tokens);
- if (oldPassword !== password) {
- console.log(password);
- }
- }
- })
- console.log(`Your password is: ${password}`);
- }
- ======================================================
- function passwordReset(input) {
- let text = input.shift();
- input.pop();
- for (let line of input) {
- let [action, ...info] = line.split(' ');
- switch (action) {
- case 'TakeOdd':
- let newPassword = '';
- let oldPasswordLength = text.length;
- for (let i = 0; i < oldPasswordLength; i++) {
- if (i % 2 !== 0) {
- newPassword += text[i];
- }
- }
- text = newPassword;
- console.log(text);
- break;
- case 'Cut':
- let [index, length] = info.map(Number);
- let substring = text.slice(index, index + length);
- text = text.replace(substring, '');
- console.log(text);
- break;
- case 'Substitute':
- let [substr, substitude] = info;
- if (text.includes(substr)) {
- while (text.includes(substr)) {
- text = text.replace(substr, substitude);
- }
- console.log(text);
- } else {
- console.log("Nothing to replace!");
- }
- break;
- }
- }
- console.log(`Your password is: ${text}`)
- }
Advertisement
Add Comment
Please, Sign In to add comment