Skip to content

final #4163

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 1 commit into
base: master
Choose a base branch
from
Open

final #4163

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
125 changes: 114 additions & 11 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,114 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}
function maxOfTwoNumbers(a, b) {
if (a > b) {
return a;
} else {
return b;
}
}



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

function findLongestWord() {}
function findLongestWord(words) {
if (words.length === 0) {
return null;
}

let longestWord = words[0];

for (let i = 1; i < words.length; i++) {
if (words[i].length > longestWord.length) {
longestWord = words[i];
}
}

return longestWord;
}



// 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;
}



// Iteration #3.1 Bonus:
function sum() {}
function sum(arr) {
let total = 0;

for (let i = 0; i < arr.length; i++) {
if (typeof arr[i] === 'number') {
total += arr[i];
} else if (typeof arr[i] === 'string') {
total += arr[i].length;
} else if (typeof arr[i] === 'boolean') {
total += arr[i] ? 1 : 0;
} else {
throw new Error('Unsupported data type');
}
}

return total;
}





// Iteration #4: Calculate the average
// Level 1: Array of numbers
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];

function averageNumbers() {}
function averageNumbers(numbers)
{
if (numbers.length === 0) return null;
const sum = numbers.reduce((acc, num) => acc + num, 0);
return sum / numbers.length;
}


// Level 2: Array of strings
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];

function averageWordLength() { }
function averageWordLength(words) {
if (words.length === 0) return null;
const totalLength = words.reduce((acc, word) => acc + word.length, 0);
return totalLength / words.length;
}




// Bonus - Iteration #4.1
function avg() {}
function avg(arr) {
if (arr.length === 0) return null; // Devuelve null si el array está vacío
const total = arr.reduce((acc, val) => {
if (typeof val === 'number') {
return acc + val;
} else if (typeof val === 'string') {
return acc + val.length;
} else if (typeof val === 'boolean') {
return acc + (val ? 1 : 0);
} else {
throw new Error('Unsupported data type');
}
}, 0);
return parseFloat((total / arr.length)); // Calcula el promedio y lo redondea a 2 decimales
}




// Iteration #5: Unique arrays
const wordsUnique = [
Expand All @@ -52,14 +125,20 @@ const wordsUnique = [
'bring'
];

function uniquifyArray() {}
function uniquifyArray(wordsUnique) {
if (wordsUnique.length === 0) return null;
return [...new Set(wordsUnique)];
}



// Iteration #6: Find elements
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];

function doesWordExist() {}
function doesWordExist(wordsFind, word) {
if (wordsFind.length === 0) return null;
return wordsFind.includes(word);
}



Expand All @@ -78,7 +157,11 @@ const wordsCount = [
'matter'
];

function howManyTimes() {}
function howManyTimes(wordsCount, word) {
if (wordsCount.length === 0) return 0;
return wordsCount.reduce((count, currentWord) => currentWord === word ? count + 1 : count, 0)

}



Expand Down Expand Up @@ -106,8 +189,28 @@ const matrix = [
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
];

function greatestProduct() {}
function greatestProduct(matrix) {
let maxProduct = 0;
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length - 3; j++) {
const product = matrix[i][j] * matrix[i][j+1] * matrix[i][j+2] * matrix[i][j+3];
if (product > maxProduct) {
maxProduct = product;
}
}

}
for (let i = 0; i < matrix.length - 3; i++) {
for (let j = 0; j < matrix[i].length; j++) {
const product = matrix[i][j] * matrix[i+1][j] * matrix[i+2][j] * matrix[i+3][j];
if (product > maxProduct) {
maxProduct = product;
}
}
}

return maxProduct;
}



Expand Down