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

function maxOfTwoNumbers(numOne,numTwo) {
if(numOne > numTwo){
return numOne
} else if (numOne < numTwo){
return numTwo
} else {
return numOne || numTwo
}
}

//console.log(maxOfTwoNumbers(10,7))

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

function findLongestWord() {}
function findLongestWord(arrayWord) {

let longWord = "";

if(arrayWord.length === 0){
return null;
}
for(let i of arrayWord){
if (i.length > longWord.length ){
longWord = i;
}
}
return longWord
}

//console.log(findLongestWord(words))


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

function sumNumbers() {}
function sumNumbers(numbers) {
let sum = 0;

for(let i of numbers){
sum += i
}
return sum
}

//console.log(numbers)

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

function averageNumbers() {}
function averageNumbers(numbers) {
if (!numbers || numbers.length === 0) {
return null;
}
let sum = 0;

for (let i of numbers){
sum += i
}
return sum / numbers.length
}

//console.log(averageNumbers(numbersAvg))


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

function averageWordLength() { }
function averageWordLength(words) {
if( !words || words.length === 0 ){
return null
}
let totalWords = 0;

for (let i of words){
totalWords += i.length;
}
return totalWords / words.length
}

//console.log(averageWordLength(wordsArr))

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

// Iteration #5: Unique arrays
const wordsUnique = [
'crab',
'poison',
'contagious',
'simple',
'bring',
'sharp',
'playground',
'poison',
'communion',
'simple',
'bring',
'crab',
'poison',
'contagious',
Expand All @@ -52,15 +117,39 @@ const wordsUnique = [
'bring'
];

function uniquifyArray() {}

function uniquifyArray(wordsUnique) {
if ( wordsUnique.length === 0 ) {
return null;
}
let uniqueArray = []

for (let i of wordsUnique) {
if (!uniqueArray.includes(i)) {
uniqueArray.push(i)
}
}
return uniqueArray
}

//console.log(uniquifyArray(wordsUnique))


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

function doesWordExist() {}
function doesWordExist( wordsFind, wordSearch ) {
if (wordsFind.length === 0){
return null;
}

if (wordsFind.indexOf(wordSearch) !== -1){
return true
} else {
return false
}
}

//console.log(doesWordExist(wordsFind, 'machine'))


// Iteration #7: Count repetition
Expand All @@ -78,8 +167,22 @@ const wordsCount = [
'matter'
];

function howManyTimes() {}
function howManyTimes(wordsCount, wordToSearch) {
if (wordsCount.length === 0) {
return 0;
}

let count = 0;

for (let i = 0; i < wordsCount.length; i++) {
if (wordsCount[i] === wordToSearch) {
count++;
}
}
return count;
}

//console.log(howManyTimes(wordsCount, 'matter'));


// Iteration #8: Bonus
Expand Down