diff --git a/.gitignore b/.gitignore index 3c3629e..64d5b53 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ node_modules +.gitignore + diff --git a/__tests__/funcArray.spec.js b/__tests__/funcArray.spec.js index 3c837c6..36f038d 100644 --- a/__tests__/funcArray.spec.js +++ b/__tests__/funcArray.spec.js @@ -297,6 +297,6 @@ test('Return 16 when all the numbers of the arrays are 2', ()=> { [20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54], [1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48] ]; - expect(greatestProduct(matrix)).toBe(51267216); + expect(greatestProduct(matrix)).toBe(70600674); }); }); diff --git a/src/funcArray.js b/src/funcArray.js index f35b09d..ee30b5f 100644 --- a/src/funcArray.js +++ b/src/funcArray.js @@ -1,15 +1,156 @@ /* Encuentra el máximo */ -export function maxOfTwoNumbers(){} -export function findLongestWord(){} -export function sumArray(){} -export function averageNumbers(){} -export function averageWordLength(){} -export function uniquifyArray(){} -export function doesWordExist(){} -export function howManyTimes(){} -export function greatestProduct(){} +export function maxOfTwoNumbers(num1, num2){ + let mayor = Math.max(num1, num2); + return mayor; +} +/* Encuentra la palabra mas larga */ +//Forma 1 de resolver +export function findLongestWord(arrayWords){ + if(arrayWords.length === 0) { + return undefined; + } else { + let longestWord=""; + arrayWords.forEach(word => { + if(word.length > longestWord.length) { + longestWord= word + } + }); + + return longestWord; +} +} +//Forma 2 de resolver +// export function findLongestWord(arrayWords){ +// let longestWord = ""; +// if(arrayWords.length === 0) { +// return undefined; +// } else { +// for( let i=0; i < arrayWords.length; i++) { +// if(arrayWords[i].length > longestWord.length) { +// longestWord = arrayWords[i]; +// } +// } +// return longestWord; +// } +// } + +/* Calcula la suma */ +//Forma guay de hacerlo +export function sumArray(nombreArray){ +const sumWithInitial = nombreArray.reduce( +(previousValue, currentValue) => previousValue + currentValue, + 0 ); + return sumWithInitial +} + +//Forma no tan guay + +// export function sumArray(nombreArray){ +// let total =0; +// for( let i= 0; i x.length) + let mediaWords= averageNumbers(largeWords); + return mediaWords; +} + +//Unique Arrays +export function uniquifyArray(array){ + if(array.length === 0) { + return undefined; + } else { let resultado = array.reduce((palabraSinRepetir, currentValue) =>{ + + if(!palabraSinRepetir.find(d => d == currentValue)) { + palabraSinRepetir.push(currentValue) + } + return palabraSinRepetir; + }, []) + + return resultado + + } +} +// Busca elementos +export function doesWordExist(array,buscar){ + if(array.includes(buscar)) + return true; + return false; +} +//Cuenta repeticiones +export function howManyTimes(array,buscar){ + if(!array.length) { + return undefined; + } else {let contador= 0; + array.forEach(element => { + if(element.includes(buscar)){ + contador ++ + } + }); + return contador;} + } + + //Cual es el producto mayor de cuatro números adyacentes? +export function greatestProduct(array){ + let max= 0; + for (let i =0; i max){ + max = array[i][j]*array[i][j+1]*array[i][j+2]*array[i][j+3]; + }; + } + } + for (let i =0; i max){ + max=array[i][j]*array[i+1][j]*array[i+2][j]*array[i+3][j] + }; + } + } + function recorrerDiagonal(array){ + for (let i =0; i max){ + max = array[i][j]*array[i+1][j-1]*array[i+2][j-2]*array[i+3][j-3] + }; + } + } + } + recorrerDiagonal(array); + let reverse= array.map(x => x.reverse()) + recorrerDiagonal(reverse); + // for (let i =0; i max){ + // max = reverse[i][j]*reverse[i+1][j-1]*reverse[i+2][j-2]*reverse[i+3][j-3] + // }; + // } + + // } + + return max + }