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
131 changes: 108 additions & 23 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,93 @@
// 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(wordsLong) {
if(wordsLong.length === 0) {
return null;
} else {
let firstOccurrence = ''
wordsLong.forEach(function(element){
if(element.length > firstOccurrence.length)
firstOccurrence = element
})
return firstOccurrence
}
}

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

function sumNumbers() {}


function sumNumbers(numbersArr) {
let sumArr = 0
numbersArr.forEach(function(element) {
sumArr += element
})
return sumArr
}

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


const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];

function sum(randomArray) {

if(randomArray.length === 0) {
return 0
}
let total = 0
randomArray.forEach(function(element){
if(element === 0) {
return 0
} else if (typeof element === 'number') {
total += element
} else if (typeof element === 'string') {
total += element.length
} else if (element === true) {
total++
} else if(element === true || typeof element === 'object') {
throw "Unsupported data type sir or ma'am"
}
})
return total
}

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

function averageNumbers() {}

// Level 1: Array of numbers
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]

function averageNumbers(numbersTest) {
if (numbersTest.length === 0) {
return null
} else {
return sumNumbers(numbersTest) / numbersTest.length
}
}

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

function averageWordLength() { }
function averageWordLength(stringArray) {
if (stringArray.length === 0) {
return null
} else {
return sum(stringArray) / stringArray.length
}
}

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

function avg(avgArr) {
if(avgArr.length === 0) {
return null
} else {
return Number((sum(avgArr) / avgArr.length).toFixed(2))
}
}

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

function uniquifyArray() {}


function uniquifyArray(uniqueArray, word) {
let newUniqueArr = []
if(uniqueArray.length === 0) {
return null
} else {
uniqueArray.forEach(function(element){
console.log(element)
if (newUniqueArr.indexOf(element) === -1) {
newUniqueArr.push(element)
}
})
}
return newUniqueArr
}

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

function doesWordExist() {}
function doesWordExist(wordsArr, word) {
if(wordsArr.length === 0) {
return null
}
let isOnArray

wordsArr.forEach(function (element) {
if (element === word) {
isOnArray = true
} else if (element ) {
isOnArray = false
}
})
return isOnArray
}



Expand All @@ -78,7 +155,15 @@ const wordsCount = [
'matter'
];

function howManyTimes() {}
function howManyTimes(wordsCount, word) {
let counter = 0
wordsCount.forEach(function (element) {
if (element === word) {
counter += 1;
}
})
return counter
}



Expand Down