https://judge.softuni.org/Contests/Practice/Index/2303#0

https://pastebin.com/NEwzmYCG

Здравейте, имам проблем със следната задача. Опитвам се да я счупя, но така и не си намирам грешката. Ако може някой да помогне : )

Благодаря предварително

Тагове:
0
Axiomatik 2422 Точки

;-)

100% code =>

``````function password(inp) {
let count = 0;
let sum = '';
let sentencetoworkat = inp.shift();

while (inp[0] != "Done") {
let [name, p1, p2] = inp.shift().split(' ');
// let name = tokens[0]
// let p1 = tokens[1]
// let p2 = tokens[2]

switch (name) {
case "TakeOdd":
for (let el of sentencetoworkat) {
if (count !== 0) {
if (count % 2 !== 0 || count == 1) {
sum += el;
}
}

count++;
}
count = 0;
sentencetoworkat = sum;
sum = '';
console.log(sentencetoworkat);

break;
case "Cut":
let index = Number(p1);
let length = Number(p2);
let substring = sentencetoworkat.substring(index, index + length);
sentencetoworkat = sentencetoworkat.replace(substring, '');
console.log(sentencetoworkat);

break;
case "Substitute":
if (sentencetoworkat.includes(p1)) {
let replaced = sentencetoworkat.split(p1).join(p2)
sentencetoworkat = replaced;

console.log(sentencetoworkat);
} else { console.log("Nothing to replace!") }

break;
}
}

}``````

1

Благодаря! Обаче е доста по различен е кода и все още не мога да си намеря грешката...

0
Axiomatik 2422 Точки

;-)

``````function passwordReset(input) {
// First, you will receive a string, and afterward, until the command "Done" is given,
// you will be receiving strings with commands split by a single space.

// The destructuring assignment syntax is a JavaScript expression that makes it possible
// to unpack values from arrays, or properties from objects, into distinct variables.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
let [line0, line1, line2] = input[0].split(' ');

while (line0 !== 'Done') {
// if (line[0] === 'Done') {
//     break;
// }

if (line0 === 'TakeOdd') {
// TakeOdd => Takes only the characters at odd indices and concatenates them to obtain
// the new raw password and then prints it.
let newPass = '';

for (let i = 1; i < finalPass.length; i = i + 2) {
newPass += finalPass[i];
// finalPass = finalPass + password[i];
}
finalPass = newPass;
console.log(finalPass);
} else if (line0 === 'Cut') {
// Cut => Gets the substring with the given length starting from the given index from the password
// and removes its first occurrence, then prints the password on the console.
let substring = finalPass.substr(Number(line1), Number(line2));
finalPass = finalPass.replace(substring, '');
console.log(finalPass);

// let oldFinalPass = finalPass.substring(0, Number(line[1]));
// finalPass = oldFinalPass + finalPass.substring((Number(line[1]) + Number(line[2])), finalPass.length);
// } else if (line[0] === 'Substitute' && finalPass.includes(line[1])) {
} else if (line0 === 'Substitute') {
// Substitute => If the raw password contains the given substring, replaces all of its occurrences
// with the substitute text given and prints the result.
if (finalPass.includes(line1) === false) {
// If it doesn't, prints "Nothing to replace!".
console.log('Nothing to replace!');
} else {
while (finalPass.includes(line1)) {
finalPass = finalPass.replace(line1, line2);
}

console.log(finalPass);
}

// finalPass = finalPass.split(line[1]);
// finalPass = finalPass.join(line[2]);
}
// else {
//     console.log('Nothing to replace!');
// }

input.shift();
[line0, line1, line2] = input[0].split(' ');

// line = input.shift().split(' ')
// if (line[0] != 'Done') {
// } else {
//     break;
// }
}