Skip to content
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
142 changes: 123 additions & 19 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,111 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}

function maxOfTwoNumbers(num1, num2) {
if (num1 > num2) {
return num1;
} else {
return num2;
}
}

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

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

function findLongestWord() {}

function findLongestWord(arr) {
if (arr.length === 0) return null;
let longestWord = '';
for (let i = 0; i < arr.length; i++ ) {
if (arr[i].length > longestWord.length) {
longestWord = arr[i];
}
}
return longestWord;
}

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(arr) {
let sum = 0;
for (let i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}

console.log(sumNumbers(numbers));

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

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

function sum(arr) {
let sum = 0;
for (let i = 0; i < arr.length; i++) {
const value = arr[i];
if (typeof value === 'object') {
throw new Error("Function does not support objects or arrays");
} else if (typeof value === 'number') {
sum += value;
} else if (typeof value === 'string') {
const strLength = value.length;
sum += strLength;
} else if (typeof value === 'boolean') {
const num = Number(value);
if (!isNaN(num)) {
sum += num;
}
}
}
return sum;
}

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() {}
function averageNumbers(arr) {
if (arr.length === 0) return null;
const sum = sumNumbers(arr);
return sum / arr.length;
}

console.log(averageNumbers(numbersAvg));

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

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

const totalLength = arr.reduce((acc, word) => acc + word.length, 0);
return totalLength / arr.length;
}

console.log(averageWordLength(wordsArr));

// Bonus - Iteration #4.1
function avg() {}
numbers = [2, 6, 9, 10, 7, 4, 1, 9];
function averageNumbers(arr) {
if (arr.length === 0) return null;

const sum = sumNumbers(arr);
return sum / arr.length;
}

console.log(averageNumbers(numbers));

function avg(arr) {
if (arr.length === 0) return null;
const totalSum = sum(arr);
return totalSum / arr.length;
}

console.log(avg(mixedArr));

// Iteration #5: Unique arrays
const wordsUnique = [
Expand All @@ -52,16 +122,29 @@ const wordsUnique = [
'bring'
];

function uniquifyArray() {}

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

console.log(uniquifyArray(wordsUnique));

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

function doesWordExist() {}
function doesWordExist(arr, word) {
if (!Array.isArray(arr) || arr.length === 0) return null;

return arr.includes(word);
}

console.log(doesWordExist(wordsFind, 'machine'));

// Iteration #7: Count repetition
const wordsCount = [
Expand All @@ -78,9 +161,17 @@ const wordsCount = [
'matter'
];

function howManyTimes() {}
function howManyTimes(arr, word) {
if (!Array.isArray(arr) || arr.length === 0) return 0;

let count = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === word) count++;
}
return count;
}

console.log(howManyTimes(wordsCount, 'matter'));

// Iteration #8: Bonus
const matrix = [
Expand All @@ -106,10 +197,23 @@ 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; j++) {
if (j + 3 < matrix[i].length) {
const horizontalProduct = matrix[i][j] * matrix[i][j + 1] * matrix[i][j + 2] * matrix[i][j + 3];
maxProduct = Math.max(maxProduct, horizontalProduct);
}
if (i + 3 < matrix.length) {
const verticalProduct = matrix[i][j] * matrix[i + 1][j] * matrix[i + 2][j] * matrix[i + 3][j];
maxProduct = Math.max(maxProduct, verticalProduct);
}
}
}
return maxProduct;
}

// The following is required to make unit tests work.
/* Environment setup. Do not modify the below code. */
Expand Down