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
79 changes: 58 additions & 21 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,71 @@
// Iteration 1 | Find the Maximum
function maxOfTwoNumbers() {}



function maxOfTwoNumbers(num1, num2) {
return num1 > num2 ? num1 : num2;
}

// Iteration 2 | Find the Longest Word
const words = ["mystery", "brother", "aviator", "crocodile", "pearl", "orchard", "crackpot"];

function findLongestWord() {}



const words = [
"mystery",
"brother",
"aviator",
"crocodile",
"pearl",
"orchard",
"crackpot",
];

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

let maxLength = 0;
let theLongestInd = -1;

arr.forEach((el, ind) => {
if (el.length > maxLength) {
maxLength = el.length;
theLongestInd = ind;
}
});
return arr[theLongestInd];
}

// Iteration 3 | Sum Numbers
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];

function sumNumbers() {}



function sumNumbers(arr) {
let sum = 0;
arr.forEach((el) => {
sum += el;
});
return sum;
}

// Iteration 4 | Numbers Average
const numbers2 = [2, 6, 9, 10, 7, 4, 1, 9];

function averageNumbers() {}



function averageNumbers(arr) {
if (arr.length === 0) return 0;
return sumNumbers(arr) / arr.length;
}

// Iteration 5 | Find Elements
const words2 = ["machine", "subset", "trouble", "starting", "matter", "eating", "truth", "disobedience"];

function doesWordExist() {}
const words2 = [
"machine",
"subset",
"trouble",
"starting",
"matter",
"eating",
"truth",
"disobedience",
];

function doesWordExist(array, word) {
if (array.length === 0) return null;

let doesExist = false;
array.forEach((el) => {
if (el === word) doesExist = true;
});
return doesExist;
}