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


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

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

function findLongestWord() {}


function findLongestWord(words) {
if (words.length === 0) {
return null;
}
let longestWord = words[0];
words.forEach(function(word) {
if (word.length > longestWord.length) {
longestWord = word;
}
});
return longestWord;
}

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

function sumNumbers() {}


function sumNumbers(numbers) {
let sum = 0;
numbers.forEach(function(element) {
sum += element;
});
return sum;
}

// Iteration #3.1 Bonus:
function sum() {}
Expand All @@ -26,13 +43,38 @@ function sum() {}
// Level 1: Array of numbers
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];

function averageNumbers() {}
function averageNumbers(numbersAvg) {
if(numbersAvg.length === 0) {
return null;
}

let sum = 0;

numbersAvg.forEach(function(element) {
sum += element;
});

const avg = sum/numbersAvg.length;

return avg;
}


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

function averageWordLength() { }
function averageWordLength(wordsArr) {
if (wordsArr.length === 0) {
return null;
}
let sum = 0;
wordsArr.forEach(function(element) {
sum += element.length;
});
const avg = sum/wordsArr.length;

return avg;
}

// Bonus - Iteration #4.1
function avg() {}
Expand All @@ -52,16 +94,34 @@ const wordsUnique = [
'bring'
];

function uniquifyArray() {}


function uniquifyArray(wordsUnique) {
if(wordsUnique.length === 0) {
return null;
}
let unique = [];
wordsUnique.forEach(function(element) {
if (unique.indexOf(element) === -1) {
unique.push(element);
}
});
return unique;
}

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

function doesWordExist() {}


function doesWordExist(wordsFind, word) {
if (wordsFind.length === 0) {
return null;
}
let words = false;
wordsFind.forEach(function(element) {
if (element === word) {
words = true;
}
});
return words;
}

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

function howManyTimes() {}

function howManyTimes(wordsCount, word) {
let counting = 0;

wordsCount.forEach(function(element) {
if (element === word) {
counting++;
}
});
return counting;
}

// Iteration #8: Bonus
const matrix = [
Expand Down