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


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

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

function findLongestWord() {}

function findLongestWord(dataArray) {
let lenArr = dataArray.length;
if (lenArr === 0) return null;
if (lenArr === 1) return dataArray[0];

let longestWord = dataArray[0];
for (i = 1; i <= lenArr - 1; i++) {
if (dataArray[i].length > longestWord.length) {
longestWord = dataArray[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(dataArray) {
let total = 0;
for (i = 0; i < dataArray.length; i++) {
total += dataArray[i];
}

return total;
}
// console.log(sumNumbers([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, [], {}]));
console.log(sumNumbers(numbers));
// Iteration #3.1 Bonus:
function sum() {}


function sum(dataArray) {
let total = 0;

for (i = 0; i < dataArray.length; i++) {
let tipe = typeof dataArray[i];
if (typeof dataArray[i] === 'object') {
throw new Error("Unsupported data type sir or ma'am");
} else if (tipe === 'number') {
total += dataArray[i];
} else if (tipe === 'string') {
total += dataArray[i].length;
} else if (tipe === 'boolean') {
dataArray[i] === true ? total++ : total;
}
}
return total;
}
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() {}

function averageNumbers(dataArray) {
let lenArr = dataArray.length;
if (lenArr === 0) return null;
let sum = 0;
for (i = 0; i < lenArr; i++) {
sum += dataArray[i];
}
return sum / lenArr;
}

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

console.log(averageWordLength(wordsArr));
// Bonus - Iteration #4.1
function avg() {}
function avg(dataArray) {
let lenArr = dataArray.length;
if (lenArr === 0) return null;
let sum = 0;
for (i = 0; i < lenArr; i++) {
let tipe = typeof dataArray[i];
if (tipe === 'number') {
sum += dataArray[i];
} else if (tipe === 'string') {
sum += dataArray[i].length;
} else if (tipe === 'boolean') {
dataArray[i] === true ? sum++ : sum;
}
}
return sum / lenArr;
}
console.log(avg([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, false]));

// Iteration #5: Unique arrays
const wordsUnique = [
Expand All @@ -52,17 +121,31 @@ const wordsUnique = [
'bring'
];

function uniquifyArray() {}

function uniquifyArray(dataArray) {
let lenArr = dataArray.length;
if (lenArr === 0) return null;
let newArr = [];
for (i = 0; i < lenArr; i++) {
let elemenAtIndex = dataArray[dataArray.indexOf(dataArray[i])];
if (newArr.includes(elemenAtIndex) === false) {
newArr.push(elemenAtIndex);
}
}
return newArr;
}

console.log(uniquifyArray(wordsUnique));

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

function doesWordExist() {}


function doesWordExist(dataArray, key) {
if (dataArray.length === 0) return null;
if (dataArray.includes(key)) return true;
else return false;
}

console.log(doesWordExist(wordsFind, 'truth'));
// Iteration #7: Count repetition
const wordsCount = [
'machine',
Expand All @@ -78,10 +161,19 @@ const wordsCount = [
'matter'
];

function howManyTimes() {}


function howManyTimes(dataArray, key) {
let lenArr = dataArray.length;
if (lenArr === 0) return 0;
let count = 0;
for (i = 0; i < lenArr; i++) {
if (dataArray[i] === key) {
count++;
}
}
return count;
}

console.log(howManyTimes(wordsCount, 'matter'));
// Iteration #8: Bonus
const matrix = [
[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
Expand All @@ -106,10 +198,49 @@ 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) {
// Ensure matrix is valid
if (!matrix || matrix.length === 0 || matrix[0].length === 0) return 0;

const rows = matrix.length;
const cols = matrix[0].length;
let maxProduct = 0; // Initialize to 0

// Check horizontal products
for (let i = 0; i < rows; i++) {
for (let j = 0; j <= cols - 4; j++) {
const product = matrix[i][j] * matrix[i][j + 1] * matrix[i][j + 2] * matrix[i][j + 3];
maxProduct = Math.max(maxProduct, product);
}
}

// Check vertical products
for (let i = 0; i <= rows - 4; i++) {
for (let j = 0; j < cols; j++) {
const product = matrix[i][j] * matrix[i + 1][j] * matrix[i + 2][j] * matrix[i + 3][j];
maxProduct = Math.max(maxProduct, product);
}
}

// Check diagonal (top-left to bottom-right) products
for (let i = 0; i <= rows - 4; i++) {
for (let j = 0; j <= cols - 4; j++) {
const product = matrix[i][j] * matrix[i + 1][j + 1] * matrix[i + 2][j + 2] * matrix[i + 3][j + 3];
maxProduct = Math.max(maxProduct, product);
}
}

// Check diagonal (top-right to bottom-left) products
for (let i = 0; i <= rows - 4; i++) {
for (let j = 3; j < cols; j++) {
const product = matrix[i][j] * matrix[i + 1][j - 1] * matrix[i + 2][j - 2] * matrix[i + 3][j - 3];
maxProduct = Math.max(maxProduct, product);
}
}

return maxProduct;
}
console.log(greatestProduct(matrix));

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