Решение на първа задача от изпита на 22.12.2023
Може би това да е вариант за решаване на задачата
function decoderCriptoInformation(informationInput) {
let criptoInformation = informationInput.shift();
for (const information of informationInput) {
const [comand, firstElement, ...lastElement] = information.split('?');
let isValid = true;
if (comand === 'TakeEven') {
//criptoInformation = criptoInformation.replace(/(.)./g, '$1');
criptoInformation = concatEvenElements(criptoInformation);
} else if (comand === 'ChangeAll') {
const searchRegExp = new RegExp(firstElement, 'g');
criptoInformation = criptoInformation.replace(searchRegExp, lastElement);
} else if (comand === 'Reverse') {
if (!criptoInformation.includes(firstElement)) {
isValid = false;
} else {
criptoInformation = criptoInformation.replace(firstElement, '');
criptoInformation = criptoInformation.concat(reverseElements(firstElement));
}
} else if (comand === 'Buy') {
console.log(`The cryptocurrency is: ${criptoInformation}`)
return;
} else {
isValid = false;
}
if (isValid) {
console.log(criptoInformation);
} else {
console.log('error')
}
}
function concatEvenElements(arr) {
let leftString = '';
let rigthString = '';
const elementLength = arr.length
for (let index = 0; index < parseInt(elementLength / 2); index++) {
let count = false
if (index % 2 == 0) {
leftString = leftString.concat(arr[index]);
if (parseInt(elementLength / 2) % 2 == 1) {
count = true;
}
if (index < parseInt(elementLength / 2) - 1) {
rigthString = rigthString.concat(arr[parseInt(elementLength / 2) + count + index]);
}
}
}
let stringEvenElements = leftString + rigthString;
if (elementLength % 2 == 1) {
stringEvenElements += arr[elementLength - 1];
}
return stringEvenElements;
}
function reverseElements(element) {
let leftString = '';
let rigthString = '';
const elementLength = element.length
for (let j = 0; j < parseInt(elementLength / 2); j++) {
leftString = leftString.concat(element[elementLength - 1 - j]);
rigthString = rigthString.concat(element[parseInt(elementLength / 2) - 1 - j]);
}
if (elementLength % 2 == 1) {
leftString += element[parseInt(elementLength / 2)];
}
return leftString + rigthString;
}
}
decoderCriptoInformation(["z2tdsfndoctsB6z7tjc8ojzdngzhtjsyVjek!snfzsafhscs",
"TakeEven",
"Reverse?!nzahc",
"ChangeAll?m?g",
"Reverse?adshk",
"ChangeAll?z?i",
"Buy"])