Skip to content

Lab Done #4228

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

Open
wants to merge 2 commits into
base: master
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
Binary file added .DS_Store
Binary file not shown.
Binary file added jasmine/.DS_Store
Binary file not shown.
112 changes: 112 additions & 0 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -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'];

Expand All @@ -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',
Expand All @@ -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 = [
Expand Down