Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#30-array-standalone #31

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
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: 52 additions & 0 deletions staff/belen-palacio/array-standalone/at.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
function at(array, index) {
if (!(typeof index === "number")) return array[0]

return array[(index > 0) ? index : array.length + index]
}

{

const result1 = at(["a", "b", "c", "d", "e"], 2);
const nativeResult1 = ["a", "b", "c", "d", "e"].at(2);
console.assert(result1 === nativeResult1, {
result: result1,
message: "Test 1 no pasado",
});

const result2 = at(["a", "b", "c", "d", "e"], -1);
const nativeResult2 = ["a", "b", "c", "d", "e"].at(-1);
console.assert(result2 === nativeResult2, {
result: result2,
message: "Test 2 no pasado",
});

const result3 = at(["a", "b", "c", "d", "e"], 10000);
const nativeResult3 = ["a", "b", "c", "d", "e"].at(10000);
console.assert(result3 === nativeResult3, {
result: result3,
message: "Test 3 no pasado",
});

const result4 = at(["a", "b", "c", "d", "e"], -100);
const nativeResult4 = ["a", "b", "c", "d", "e"].at(-100);
console.assert(result4 === nativeResult4, {
result: result4,
message: "Test 4 no pasado",
});

const result5 = at(["a", "b", "c", "d", "e"], null);
const nativeResult5 = ["a", "b", "c", "d", "e"].at(null);
console.assert(result5 === nativeResult5, {
result: result5,
message: "Test 5 no pasado",
});

const result6 = at(["a", "b", "c", "d", "e"], "a");
const nativeResult6 = ["a", "b", "c", "d", "e"].at("a");
console.assert(result6 === nativeResult6, {
result: result6,
message: "Test 6 no pasado",
});

}

58 changes: 58 additions & 0 deletions staff/belen-palacio/array-standalone/concat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// funcion que concatena dos arrays y devuelve un nuevo array.

function concat() {
let newArray = [];

for (let i = 0; i < arguments.length; i++) { // un for dentro de un for
for (let j = 0; j < arguments[i].length; j++) {
newArray[newArray.length] = arguments[i][j] // al nuevo array le agrego ambos
}
}

return newArray
}

{
function isArrayEqual(arr1, arr2) {
if (arr1.length !== arr2.length) return false;

let result = true;
let i = 0;
while (i < arr1.length || result === false) {
if (arr1[i] !== arr2[i]) result = false;
i++;
}
return result;
}

const array1 = ["a", "b"];
const concatArray1 = ["c", "d"];
const result1 = concat(array1, concatArray1); // ['a', 'b', 'c', 'd']
const nativeResult1 = array1.concat(concatArray1); // ['a', 'b', 'c', 'd']

console.assert(isArrayEqual(result1, nativeResult1), {
result: result1,
message: "Test 1 no pasado",
});

const array2 = [1, 2];
const concatArray2 = [2, 1];
const result2 = concat(array2, concatArray2);
const nativeResult2 = array2.concat(concatArray2);

console.assert(isArrayEqual(result2, nativeResult2), {
result: result2,
message: "Test 2 no pasado",
});

const array3 = [1, 2];
const concatArray3 = [2, 1];
const concatArray3b = [2, 1];
const result3 = concat(array3, concatArray3,concatArray3b);
const nativeResult3 = array2.concat(concatArray3, concatArray3b);

console.assert(isArrayEqual(result3, nativeResult3), {
result: result3,
message: "Test 3 no pasado",
});
}
68 changes: 68 additions & 0 deletions staff/belen-palacio/array-standalone/copy-within.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
function copyWithin(array, target = 0, start = 0, end = array.length) {
for (let i = start; i < end; i++) array[target + i - start] = array[i];

return array;
}

{
function isArrayEqual(arr1, arr2) {
if (arr1.length !== arr2.length) return false;

let result = true;
let i = 0;
while (i < arr1.length || result === false) {
if (arr1[i] !== arr2[i]) result = false;
i++;
}
return result;
}
}

{
const array1 = [1, 2, 3, 4, 5];
const nativeArray1 = [1, 2, 3, 4, 5];
const result1 = copyWithin(array1, 0, 3, 5); // [4, 5, 3, 4, 5]
const nativeResult1 = nativeArray1.copyWithin(0, 3, 5);

console.assert(isArrayEqual(result1, nativeResult1), {
result: result1,
message: "Test 1.1 no pasado",
});

console.assert(array1 === result1, {
result: result1,
message: "Test 1.2 no pasado",
});

const array2 = [1, 2, 3, 4, 5, 6];
const nativeArray2 = [1, 2, 3, 4, 5, 6];
const result2 = copyWithin(array2, -4, -3, -1); // [1, 2, 4, 5, 5, 6]
const nativeResult2 = nativeArray2.copyWithin(-4, -3, -1);

console.assert(isArrayEqual(result2, nativeResult2), {
result: result2,
message: "Test 2.1 no pasado",
});

console.assert(array2 === result2, {
result: result2,
message: "Test 2.2 no pasado",
});

const array3 = [10, 20, 30, 40, 50]; // parametro de start y end implicitos
const nativeArray3 = [10, 20, 30, 40, 50];
const result3 = copyWithin(array3, 2); // [10, 20, 10, 20, 30] copio desde la posicion 2, es decir el 30
const nativeResult3 = nativeArray3.copyWithin(2);

console.assert(isArrayEqual(result3, nativeResult3), {
result: result3,
message: "Test 3.1 no pasado",
});

console.assert(array3 === result3, {
result: result3,
message: "Test 3.2 no pasado",
});

}

55 changes: 55 additions & 0 deletions staff/belen-palacio/array-standalone/every.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// El método every prueba si todos los elementos pasan la prueba
// implementada por la función proporcionada.
// Devuelve true or false

function every(array, callback) {
let result = true
let i = 0;

while (i < array.length && result) { // siempre y cuando i sea menos que array.length y result true? estoy dentro del while
if (!callback(array[i], i, array)) {
result = false; // si encuentro un elemento que no cumple la condición, cambiamos result a false
}

i++;

}

return result // true or false
}
{

function isArrayEqual(arr1, arr2) {
if (arr1.length !== arr2.length) return false;

let result = true;
let i = 0;
while (i < arr1.length || result === false) {
if (arr1[i] !== arr2[i]) result = false;
i++;
}
return result;
}

const array1 = [11, 12, 21, 22, 100];
const fn1 = (num) => num > 10;
const result1 = every(array1, fn1); // en este caso vamos a buscar si los numeros son mayores a 10
const nativeResult1 = array1.every(fn1); // true todos

console.assert(isArrayEqual(result1, nativeResult1), {
result: result1,
message: "Test 1 no pasado",
});

const array2 = [0, 1, -1];
const fn2 = (element) => num = 10;
const result2 = every(array2, fn2);
const nativeResult2 = array2.every(fn2);

console.assert(isArrayEqual(result2, nativeResult2), {
result: result2,
message: "Test 2 no pasado",
});

}

33 changes: 33 additions & 0 deletions staff/belen-palacio/array-standalone/fill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// método que rellena todos los elementos de un array desde
// una posición de inicio hasta una posición final con un valor estático

function fill(array, value, start = 0, end = array.length) {


for (let i = start; i < end; i++) { // para recorrerlo desde q inicia hasta q termina
array[i] = value; // cambio el valor al que le di
}

return array
}

function isArrayEqual(arr1, arr2) {
if (arr1.length !== arr2.length) return false;

let result = true;
let i = 0;
while (i < arr1.length || result === false) {
if (arr1[i] !== arr2[i]) result = false;
i++;
}
return result;
}

const array1 = [1, 2, 3, 4];
const result1 = fill(array1, 0); // [0, 0, 0, 0]
const nativeArray1 = [1, 2, 3, 4];
const nativeResult1 = nativeArray1.fill(0);
console.assert(isArrayEqual(result1, nativeResult1), {
result: result1,
message: "Test 1 no pasado"
});
69 changes: 69 additions & 0 deletions staff/belen-palacio/array-standalone/find-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// El método find() devuelve el valor del primer elemento
// del array que cumple la función de prueba proporcionada

// si el metodo esta devuelve eso si no, -1

function findIndex(array, callback) {
let result = -1 // le damos valor -1 inicial
let i = 0



while (i < array.length && result === -1) { // si no encontramos valor, es decir que sea === undefined
if (callback(array[i], i, array)) {
result = i;
}

i++;
}

return result; // si no -1. no se encontro nada
}

{

function isArrayEqual(arr1, arr2) {
if (arr1.length !== arr2.length) return false;

let result = true;
let i = 0;
while (i < arr1.length || result === false) {
if (arr1[i] !== arr2[i]) result = false;
i++;
}
return result;
}

{

const array1 = [1, 2, 3];
const fn1 = (element) => element >= 2; // 2
const result1 = findIndex(array1, fn1);
const nativeResult1 = array1.findIndex(fn1);

console.assert(result1 === nativeResult1, {
result: result1,
message: "Test 1 no pasado",
});

const array2 = [10, 50, 300];
const fn2 = (element) => element >= 2;
const result2 = findIndex(array2, fn2);
const nativeResult2 = array2.findIndex(fn2);

console.assert(result2 === nativeResult2, {
result: result2,
message: "Test 2 no pasado",
});

const array3 = [10, 50, 300];
const fn3 = (element) => element < 2;
const result3 = findIndex(array3, fn3);
const nativeResult3 = array3.findIndex(fn3);

console.assert(result3 === nativeResult3, {
result: result3,
message: "Test 3 no pasado",
});

}}
Loading