Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions projects/007-run-length-encoding/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const prompt = require('prompt-sync')();

/**
*
* @param {array} decompressedList
* @returns A function that maps each individual element of the array and counts how many duplicates there are.
*/
function compressedList(decompressedList) {
if (!decompressedList) return []; // Base case: empty array

const compressedArray = []; // Empty array to insert each repeated element

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.

// Transformed the message typed by the user into an array and arranged each character alphabetically

console.log(compressedList(decompressedList));