Skip to content

exercises functions-arrays #4231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
131 changes: 121 additions & 10 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,127 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}
function maxOfTwoNumbers(num1, num2) {
if (num1 > num2) {
return num1;
} else if (num1 < num2) {
return num2;
} else {
return num1;
}
}



// 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 longest = words[0];

for (let i = 1; i < words.length; i++) {
if (words[i].length > longest.length) {
longest = words[i];
}
}

return longest;
}



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

function sumNumbers() {}
function sumNumbers(numbers) {
if (numbers.length === 0) {
return 0;
}

let sum = 0;

for (let i = 0; i < numbers.length; i++) {
sum += numbers[i]
}
return sum
}



// Iteration #3.1 Bonus:
function sum() {}
function sum(numbers) {
if (!numbers.length) return 0;

let total = 0;

for (let item of numbers) {
if (typeof item === 'number') {
total += item;
} else if (typeof item === 'string') {
total += item.length;
} else if (typeof item === 'boolean') {
total += item ? 1 : 0;
} else {
throw new Error("no data type availbale")
}
}

return total;
}





// Iteration #4: Calculate the average
// 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 total = 0;

for (let num of numbersAvg) {
total += num;
}
return total / numbersAvg.length;
}


// 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 average = 0;

for ( let word of wordsArr) {
average += word.length;
}
return average/wordsArr.length;
}

// Bonus - Iteration #4.1
function avg() {}
function avg(wordsArr) {
if (wordsArr.length === 0) return null;

let total = 0;

for (let item of wordsArr) {
if (typeof item === 'number') {
total += item;
} else if (typeof item === 'string') {
total += item.length;
} else if (typeof item === 'boolean') {
total += item ? 1 : 0;
}
}
return total/wordsArr.length;
}


// Iteration #5: Unique arrays
const wordsUnique = [
Expand All @@ -52,15 +138,29 @@ const wordsUnique = [
'bring'
];

function uniquifyArray() {}
function uniquifyArray(wordsUnique) {
if (wordsUnique.length === 0) return null;

let newWordsUnique = [];

for (let word of wordsUnique) {
if(!newWordsUnique.includes(word)) {
newWordsUnique.push(word);
}
}
return newWordsUnique;
}



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

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

return wordsFind.includes(wordToSearch);
}


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

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

let count = 0;

for (let word of wordsCount) {
if (word === wordToCount) {
count++;
}
}
return count;
}



Expand Down