diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 000000000..b9ef05e2b Binary files /dev/null and b/.DS_Store differ diff --git a/jasmine/.DS_Store b/jasmine/.DS_Store new file mode 100644 index 000000000..32fef224a Binary files /dev/null and b/jasmine/.DS_Store differ diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 720d3dcf5..c4c321797 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,16 +1,96 @@ // Iteration #1: Find the maximum +function maxOfTwoNumbers(num1, num2){ + return Math.max(num1, num2); +} + // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; +function findLongestWord(wordsArray){ + if (wordsArray.length === 0){ + return null; + } + let tam = 0; + let palabraGanadora = null; + for(let i = 0; i < wordsArray.length; i++){ + // contar el tamaño de la string + if(wordsArray[i].length > tam){ + tam = wordsArray[i].length; + palabraGanadora = wordsArray[i]; + } + } + return palabraGanadora; + //Iteración + //Encontrar la palabra más grande + // retornar palabra más grande + +}; + // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; +/* function sumNumbers(arrayNumbers){ + if (arrayNumbers.length === 0){ + return 0; + } + let suma = 0; + for (let i = 0; i < arrayNumbers.length; i++){ + suma += arrayNumbers[i] + } +return suma; +}; */ + +function sumNumbers(arrayNumbers){ + if (arrayNumbers.length === 0){ + return 0; + } + + let resultado = sum(arrayNumbers); + + return resultado; +}; + + +function sum (arr){ + let suma = 0; + for(let i = 0; i < arr.length; i++){ + if( typeof arr[i] === "number"){ + suma += arr[i] + + } else if(typeof arr[i] === "boolean"){ + suma += arr[i] + + } else if(typeof arr[i] === "string"){ + suma += arr[i].length + + } + } + return suma; +}; + + + + + // Iteration #4: Calculate the average // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; + function averageNumbers(num){ + + // probar si es vacío y retornar null + if (num.length == 0){ + return null; + + let average = sumNumbers(num) / num.length; + return average; + } + }; + + + // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; @@ -29,9 +109,29 @@ const wordsUnique = [ 'bring' ]; +function uniquifyArray (arr){ + let newArr = []; + for(let i = 0; i < arr.length; i++){ + if (newArr.indexOf(arr[i].toLowerCase())===-1) newArr.push(arr[i].toLowerCase()); + } + return newArr; +}; + // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; +function doesWordExist(words, wordToSearch){ + if (words.length === 0) { + return null; + } + for (let i = 0; i< words.length; i++){ + if (words[i].includes(wordToSearch)){ + return true + } else return false + } +}; + + // Iteration #7: Count repetition const wordsCount = [ 'machine', @@ -47,6 +147,18 @@ const wordsCount = [ 'matter' ]; +function howManyTimes(arrayNumbers, wordToSearch){ + if (arrayNumbers.length === 0) { + return null; + } +let wordTimes= 0; + for (let i = 0; i< arrayNumbers.length; i++){ + if( arrayNumbers[i] === wordToSearch) + wordTimes++ + } + return wordTimes; +}; + // Iteration #8: Bonus const matrix = [