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

function maxOfTwoNumbers(num1, num2) {
return Math.max(num1, num2);
}


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

function findLongestWord() {}

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

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


// Iteration #3: Calculate the sum

const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];

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



Expand All @@ -26,16 +45,37 @@ function sum() {}
// Level 1: Array of numbers
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];

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


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

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

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

function avg() {

}

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

function uniquifyArray() {}
function uniquifyArray(words) {
if (words.length === 0) {
return null;
}
let deduplicatedArray = [];
for (let i = 0; i < words.length; i++) {
if (deduplicatedArray.indexOf(words[i]) === -1) {
deduplicatedArray.push(words[i]);
}
}
return deduplicatedArray;
}



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

function doesWordExist() {}
function doesWordExist(wordArr, wordFind) {
if (wordArr === 0){
return null;
}
return wordArr.included(wordFind);
}



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

function howManyTimes() {}
function howManyTimes(inputArray, countWord) {

let sum = 0;
for (let i = 0; i < inputArray.length; i++) {
if (inputArray[i] === countWord) {
sum += 1;
}
}
return sum;
}


// Iteration #8: Bonus
Expand All @@ -106,7 +170,42 @@ 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(bigArray) {
let maxProduct = 0;

// Find the greatest product of four adjacent numbers horizontally
for (let i = 0; i < bigArray.length; i++) {
for (let j = 0; j < bigArray[i].length - 3; j++) {
let product =
bigArray[i][j] *
bigArray[i][j + 1] *
bigArray[i][j + 2] *
bigArray[i][j + 3];
if (product > maxProduct) {
maxProduct = product;
}
}
}

// Find the greatest product of four adjacent numbers vertically
for (let i = 0; i < bigArray.length - 3; i++) {
for (let j = 0; j < bigArray[i].length; j++) {
let product =
bigArray[i][j] *
bigArray[i + 1][j] *
bigArray[i + 2][j] *
bigArray[i + 3][j];
if (product > maxProduct) {
maxProduct = product;
}
}
}

return maxProduct;
}

const result = greatestProduct(matrix);
console.log(result);



Expand Down