Skip to content

Solved lab #4230

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
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
91 changes: 81 additions & 10 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,90 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}
function maxOfTwoNumbers( a, b ) {
return (a>b ) ? a : 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;
return words.reduce((longest, current) =>
current.length > longest.length ? current : longest
);
}



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

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



// Iteration #3.1 Bonus:
function sum() {}
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];

function sum(mixedArr) {
let totalSum = 0;
if (mixedArr.length === 0) return 0;
for (let i = 0; i < mixedArr.length; i++) {
if (typeof mixedArr[i] === 'number') {
totalSum += mixedArr[i];
} else if (typeof mixedArr[i] === 'string') {
totalSum += mixedArr[i].length;
} else if (typeof mixedArr[i] === 'boolean') {
totalSum += mixedArr[i] ? 1 : 0;
} else {
throw new Error("Error message goes here");
}
}
return totalSum;
}



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

function averageNumbers() {}
function averageNumbers(numbersAvg) {
if (numbersAvg.length === 0) return null;
let sum = sumNumbers(numbersAvg);
return sum / numbersAvg.length;
}


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

function averageWordLength() { }
function averageWordLength(wordsArr) {
if (wordsArr.length === 0) return null;
let totalLength = 0;
if (wordsArr.length > 0) {
for (let i = 0; i < wordsArr.length; i++) {
totalLength += wordsArr[i].length;
}
return totalLength / wordsArr.length;
}
};


// Bonus - Iteration #4.1
function avg() {}
function avg(mixedArr) {
if (mixedArr.length === 0) return null;
let total = sum(mixedArr);
return total / mixedArr.length;
}

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

function uniquifyArray() {}
function uniquifyArray(wordsUnique) {
if (wordsUnique.length === 0) return null;
let uniqueWords = [];
for (let i = 0; i < wordsUnique.length; i++) {
if (!uniqueWords.includes(wordsUnique[i])) {
uniqueWords.push(wordsUnique[i]);
}
}
return uniqueWords
}



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

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




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

function howManyTimes() {}
function howManyTimes(wordsCount, word) {
if (!wordsCount || wordsCount.length === 0) return 0;
let count = 0;
for (let i = 0; i < wordsCount.length; i++) {
if (wordsCount[i] === word) {
count++;
}
}
return count;
}



Expand Down