Skip to content

Conversation

AlessandroVerzella
Copy link
Collaborator

Questa funzione prende come parametro un array, i cui elementi sono stati ordinati in ordine crescente con il metodo sort(), per facilitare la lettura degli item compressati nel nuovo array.

Ho utilizzato il metodo forEach per mappare ogni singolo item dell'array, evitando di utilizzare il for loop.

Il caso base è rappresentato da un eventuale array vuoto, mentre il passo ricorsivo è il forEach() utilizzato per mappare l'array stesso e numerare gli item duplicati.


decompressedList.forEach(
(item) => (compressedArray[item] = (compressedArray[item] || 0) + 1)
); // Recursive step

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a recursive step but a loop.
Review your implementation because it is incorrect. ;-)

return compressedArray;
}

const decompressedList = [...prompt('Enter what you desire')].sort();
Copy link

@aleattene aleattene Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you enter AAABBBAAA?

You get A:6, B:3 when you should get A, 3, B, 3, A, 3.

Theoretically, you should start with an array ... compress it (exe 006) and decompress it (exe 007) obtaining in the end the same starting array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants