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


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

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

function findLongestWord() {}


function findLongestWord(arrayOfWords) {
if (arrayOfWords.length === 0) {
return null;
}
let longestWord = '';
// for (let i=0; i<arrayOfWords; i++) { const currentWord = aarrayOfWords[i]}
for (const currentWord of arrayOfWords) {
if (longestWord.length < currentWord.length) {
longestWord = currentWord;
}
}
return longestWord;
}

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

function sumNumbers() {}


function sumNumbers(numArray) {
let totalSum = 0;
for (const number of numArray) {
totalSum += number;
}
return totalSum;
}

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


function sum(mixedArray) {
let mixedSum = 0;
for (const item of mixedArray) {
if (typeof item === 'string') {
mixedSum += item.length;
} else if (typeof item === 'number') {
mixedSum += item;
} else if (typeof item === 'boolean') {
mixedSum += Number(item);
} else {
throw new Error("Unsupported data type sir or ma'am");
}
}
return mixedSum;
}

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

function averageNumbers() {}

function averageNumbers(numArray) {
if (!numArray.length) {
return null;
}
const totalSum = sumNumbers(numArray);
return totalSum / numArray.length;
}

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

function averageWordLength() { }

function averageWordLength(wordsArray) {
if (!wordsArray.length) {
return null;
}
let average = 0;
for (const words of wordsArray) {
average += words.length / wordsArray.length;
}
return average;
}
// Bonus - Iteration #4.1
function avg() {}
function avg(arr) {
if (!arr.length) {
return null;
}
/* for (const mix of arr) {
if (typeof mix === 'string') {
aver += mix.length;
} else if (typeof mix === 'number') {
aver += mix;
} else if (typeof mix === 'boolean') {
aver += Number(mix);
}*/
const aver = mixedSum;

return aver;
}

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

function uniquifyArray() {}


function uniquifyArray(wordArr) {
if (!wordArr.length) {
return null;
}
const uniqueArr = [];
for (let i = 0; i < wordArr.length; i++) {
const index = wordArr.indexOf(wordArr[i]);
if (index === i) {
uniqueArr.push(wordArr[i]);
}
}
return uniqueArr;
}

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

function doesWordExist() {}


function doesWordExist(words, word) {
if (!words.length) {
return null;
}
return words.includes(word);
/* my solution did not work!!, why?
words = [];
for (let i = 0; i < words.length; i++)
if (word === words[i]) {
return true;
}
return false;*/
}

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

function howManyTimes() {}


function howManyTimes(wordsCount, search) {
if (!wordsCount) {
return null;
}
let count = 0;
for (const word of wordsCount) {
if (word === search) {
count++;
}
}
return count;
}

// Iteration #8: Bonus
const matrix = [
Expand Down Expand Up @@ -108,9 +194,6 @@ const matrix = [

function greatestProduct() {}




// The following is required to make unit tests work.
/* Environment setup. Do not modify the below code. */
if (typeof module !== 'undefined') {
Expand Down