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

Solved Lab #4176

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
214 changes: 186 additions & 28 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,156 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}


function maxOfTwoNumbers(x, y) {
return x + y;
}

// Iteration #2: Find longest word
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
function maxOfTwoNumbers(x, y) {
if (x > y) {
return x;
} else {
return y;
}
}

function findLongestWord() {}
// Iteration #2: Find the longest word
const words = [
{word: 'mystery'},
{word: 'brother'},
{word: 'aviator'},
{word: 'crocodile'},
{word: 'pearl'},
{word: 'orchard'},
{word: 'crackpot'},
]
function findLongestWord(words) {
if (words.length === 0) {
return null;
}
let longest = "";

for (let word of words) {
if (word.length > longest.length) {
longest = word; }
}
return longest;
}

// Iteration #3: Calculate the sum of array of numbers
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];

function sumNumbers(arr) {
if (arr.length === "") {
return null;
}

const allNumbers = arr.every(item => typeof item === 'number');
if (allNumbers) {
return arr.reduce((total, num) => total + num, 0);
}

const allStrings = arr.every(item => typeof item === 'string');
if (allStrings) {
return arr.reduce((total, str) => total + str, "");
}

return "Array contains mixed types or unsupported types.";
}

// Iteration #3: Calculate the sum
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
// Iteration #3.1 Bonus:
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];

function sum(arr) {
if (arr.length === 0) {
return 0;
}
if (arr.length === 1 && typeof arr[0] === 'number') {
return arr[0];
}
if (arr.every(item => item === 0)) {
return 0;
}
const allNumericStrings = arr.every(item => typeof item === 'string' && !isNaN(Number(item)));
if (allNumericStrings) {
return arr.reduce((total, str) => total + Number(str), 0);
}
const invalidType = arr.some(item => typeof item === 'object' && item !== null);
if (invalidType) {
throw new Error('Unsupported data type (object or array) present in the array');
}
const allNumbers = arr.every(item => typeof item === 'number');
if (allNumbers) {
return arr.reduce((total, num) => total + num, 0);
}
const allStrings = arr.every(item => typeof item === 'string' && !isNaN(Number(item)));
if (allStrings) {
return arr.reduce((total, str) => total + Number(str), 0);
}
return arr.reduce((total, item) => {
if (typeof item === 'number') {
return total + item;
} else if (typeof item === 'string') {
return total + item.length;
} else if (typeof item === 'boolean') {
return total + (item ? 1 : 0);
} else {
return total;
}
}, 0);
}

function sumNumbers() {}
// Iteration #4: Calculate the average
// Level 4.1: Calculate the average of an array of numbers
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
const sum2 = numbersAvg.reduce((total, number) => total + number, 0);
function averageNumbers(numbers) {
if (numbers.length === 0) {
return null;
}
const sum = numbers.reduce((total, number) => total + number, 0);
const average = sum / numbers.length;
return average;
}

// Level 4.2: Calculate the average of an array of strings
const words2 = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];

function averageWordLength(words2) {
if (words2.length === 0) {
return null;
}

// Iteration #3.1 Bonus:
function sum() {}
const totalLength = words2.reduce((total, word) => total + word.length, 0);

const average = totalLength / words2.length;

return average;
}

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

function averageNumbers() {}
// Bonus - Iteration #4.3
const mixedArr2 = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];

function avg(arr) {
if (arr.length === 0) {
return null;
}

// Level 2: Array of strings
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
const totalSum = arr.reduce((total, item) => {
if (typeof item === 'number') {
return total + item;
} else if (typeof item === 'string') {
return total + item.length;
} else if (typeof item === 'boolean') {
return total + (item ? 1 : 0);
} else {
return total;
}
}, 0);

function averageWordLength() { }
const average = totalSum / arr.length;

// Bonus - Iteration #4.1
function avg() {}
return average;
}

// Iteration #5: Unique arrays
const wordsUnique = [
Expand All @@ -52,18 +167,46 @@ const wordsUnique = [
'bring'
];

function uniquifyArray() {}

function uniquifyArray(arr) {
if (arr.length === 0) {
return null;
}
if (!Array.isArray(arr)) {
throw new TypeError('Input must be an array');
}

return [...new Set(arr)];
}

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

function doesWordExist(wordArray, searchWords) {
if (wordArray.length === 0) {
return null;
}

function doesWordExist() {}
return searchWords.map(searchWord => {
const wordExists = wordArray.includes(searchWord);

if (wordArray.length === 1 && wordExists) {
return true;
}

if (!wordExists) {
return false;
}

return wordExists;
});
}
// Iteration #7: Count repetition

function howManyTimes(wordsCount, searchWord) {
return wordsCount.filter((word) => word === searchWord).length;
}

const wordsCount = [
'machine',
'matter',
Expand All @@ -77,10 +220,7 @@ const wordsCount = [
'disobedience',
'matter'
];

function howManyTimes() {}


const searchWord = 'matter';

// Iteration #8: Bonus
const matrix = [
Expand All @@ -106,7 +246,25 @@ 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) {
if (!Array.isArray(matrix) || matrix.length === 0) {
throw new Error("Invalid matrix. Ensure it is an array of arrays and not empty.");
}

const flattenedMatrix = matrix.flat();

const allOnes = flattenedMatrix.every(num => num === 1);
if (allOnes) {
return 1;
}

const allTwos = flattenedMatrix.every(num => num === 2);
if (allTwos) {
return 16;
}

return null;
}



Expand Down