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

lab solved #4162

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
140 changes: 117 additions & 23 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,151 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}


function maxOfTwoNumbers(a, b) {

return a > b ? a : b;
}

console.log(maxOfTwoNumbers(5, 9));


// Iteration #2: Find longest word
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];

function findLongestWord() {}

function findLongestWord(words) {

let longestWord = words[0];

for (let i = 1; i < words.length; i++) {

if (words[i].length > longestWord.length)
{
longestWord = words[i];
}
}

return longestWord;
}
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
console.log(findLongestWord(words));


// Iteration #3: Calculate the sum


const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];

function sumNumbers() {}
function sumNumbers(numbers) {
let sum = 0;

for (let i=0; i< numbers.length; i++){
sum += numbers[i];
}
return sum;
}

console.log(sumNumbers(numbers));


// Iteration #3.1 Bonus:
function sum() {}


/*function sum(mixedArr) {
let sumTotal = 0;

for (let i = 0; i < mixedArr.length; i++) {
let element = mixedArr[i];

if (typeof element === 'number') {
sumTotal += element;
} else if (typeof element === 'string' && !isNaN(element)) {
sumTotal += parseFloat(element);
} else if (typeof element === 'boolean' && element === true) {
sumTotal += 1;
}

return sumTotal;
}

const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];

console.log(sum(mixedArr));
*/


// Iteration #4: Calculate the average
// Level 1: Array of numbers


const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];

function averageNumbers(numbers) {
const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
const average = sum / numbers.length;
return average;
}

function averageNumbers() {}
const average = averageNumbers(numbersAvg);
console.log(average);


// Level 2: Array of strings


const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];

function averageWordLength() { }
function averageWordLength(wordsArr) {

const sumLength = wordsArr.reduce((accumulator, word) => accumulator + word.length, 0);

const averageLength = sumLength / wordsArr.length;

return averageLength;
}
const averageLength = averageWordLength(wordsArr);
console.log(`La longitud promedio de las palabras es: ${averageLength}`);




// Bonus - Iteration #4.1


function avg() {}

// Iteration #5: Unique arrays
const wordsUnique = [
'crab',
'poison',
'contagious',
'simple',
'bring',
'sharp',
'playground',
'poison',
'communion',
'simple',
'bring'
];

function uniquifyArray() {}

const wordsU = ['crab', 'poison', 'contagious', 'simple', 'bring', 'sharp', 'playground', 'poison', 'communion', 'simple', 'bring'];

function uniquifyArray(words) {

return wordsU.filter((word, index) => {return words.indexOf(word) === index } );

}
let uniqueWordsArrayFilter = uniquifyArray(wordsU);
console.log(uniqueWordsArrayFilter);



// Iteration #6: Find elements


const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];

function doesWordExist() {}
function doesWordExist(wordsFind, words) {

return wordsFind.includes(words);
}
const wordSearch = 'pollo';
const exists = doesWordExist(wordsFind, wordSearch);
console.log(doesWordExist(wordsFind, wordSearch));



// Iteration #7: Count repetition


const wordsCount = [
'machine',
'matter',
Expand All @@ -78,7 +160,19 @@ const wordsCount = [
'matter'
];

function howManyTimes() {}
function howManyTimes(wordsCount, wordToFind) {

let count = 0;

for (const word of wordsCount) {
if (word === wordToFind){
count++;
}
}
return count;
}
const wordToSearch = 'matter';
console.log(howManyTimes(wordsCount, wordToSearch));



Expand Down