83/100 01. Programming Fundamentals Final Exam Retake
Здравейте, решавам стари изпитни задачи и на тази:
https://judge.softuni.org/Contests/Practice/Index/2525#2
получавам 83/100.
Кода ми е доста засукан и ще се радвам за помощ да разбера къде точно греша или кой случай не обхващам.
function solve(input) {
let pieceCount = Number(input.shift());
let collection = input.slice(0,pieceCount);
let commands = input.slice(3);
let line = commands.shift();
while(line != "Stop"){
let [command, piece, composer, key] = line.split("|");
if(command == "ChangeKey"){
key = composer;
}
switch (command) {
case "Add":{
checkPieceAndAdd(collection,line);
break;
}
case "Remove":{
removePiece(collection, piece);
break;
}
case "ChangeKey":{
changeKey(collection,piece, key);
break;
}
}
line = commands.shift();
}
function checkPieceAndAdd(collection,line) {
let lineWithOutCommand = line.split("Add|");
let [piece,composer,key] = lineWithOutCommand[1].split("|")
let isIn = false;
for(let i = 0 ; i < collection.length;i++){
let currentPiece = collection[i].split("|")[0];
if(currentPiece === piece){
isIn = true;
break;
}
}
if(!isIn){
console.log(`${piece} by ${composer} in ${key} added to the collection!`);
collection.push(lineWithOutCommand[1]);
}else if(isIn){
console.log(`${piece} is already in the collection!`);
}
}
function removePiece(collection,piece) {
let isIn = false;
for(let i = 0; i < collection.length;i++){
let currentLine = collection[i].split("|")
if(currentLine.includes(piece)){
isIn = true;
collection.splice(i,1);
break;
}
}
if(isIn){
console.log(`Successfully removed ${piece}!`);
}else{
console.log(`Invalid operation! ${piece} does not exist in the collection.`);
}
}
function changeKey(collection,piece,key){
let isIn = false;
for(let i = 0; i < collection.length;i++){
let currentLine = collection[i].split("|")
if(currentLine.includes(piece)){
isIn = true;
currentLine[2] = key;
collection[i] = currentLine.join("|")
break;
}
}
if(isIn){
console.log(`Changed the key of ${piece} to ${key}!`);
}else{
console.log(`Invalid operation! ${piece} does not exist in the collection.`);
}
}
collection.sort((a,b)=> {
if(a > b){
return 1;
}if(a < b){
return -1;
} return 0;
});
collection.forEach(element => {
let [piece, composer, key] = element.split("|");
console.log(`${piece} -> Composer: ${composer}, Key: ${key}`);
});
}
Благодаря,не мога да повярвам каква грашка елементарна не можах да намеря толкова дълго.