Skip to content
Open
Show file tree
Hide file tree
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
52 changes: 45 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,55 @@

// Esta clase SortedList es básicamente una lista de números que siempre se mantiene ordenada sola.
class SortedList {
constructor() {}
// El constructor arranca la lista vacía y el length en 0
constructor() {
this.items = [];
this.length = 0;
}

add(item) {}
// Puedes agregar números y automáticamente se acomodan de menor a mayor.
add(item) {
this.items.push(item);
this.items.sort((a, b) => a - b);
this.length = this.items.length;
}

get(pos) {}
// Si pides una posición que no existe, te avienta un error.
get(pos) {
if (pos < 0 || pos >= this.items.length) {
throw new Error("OutOfBounds");
}
return this.items[pos];
}

max() {}
// Saca el mayor de la lista, pero si está vacía, error.
max() {
if (this.items.length === 0) {
throw new Error("EmptySortedList");
}
return this.items[this.items.length - 1];
}

min() {}
// Saca el menor de la lista, pero si está vacía, error.
min() {
if (this.items.length === 0) {
throw new Error("EmptySortedList");
}
return this.items[0];
}

sum() {}
// Suma todos los elementos. Si está vacía, regresa 0.
sum() {
return this.items.reduce((acc, val) => acc + val, 0);
}

avg() {}
// Saca el promedio. Si está vacía, error.
avg() {
if (this.items.length === 0) {
throw new Error("EmptySortedList");
}
return this.sum() / this.items.length;
}
}

module.exports = SortedList;
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
"intro"
],
"author": "fer@ironhack.com",
"license": "MIT"
"license": "MIT",
"dependencies": {
"mocha": "^11.7.5"
}
}