Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
done w/o Bonus-Iteration 8 & 8.1
  • Loading branch information
phpaul89 committed Mar 24, 2020
commit 38a48d1d98a5093499d9a707db62d85fe7800d4d
120 changes: 120 additions & 0 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,103 @@
// Iteration #1: Find the maximum

function maxOfTwoNumbers(a,b){
if(a > b){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can rather write: if(a >= b) {return a}
This way you dont need to make the else statement:)

return a;
} else if (a < b){
return b;
} else {
return a;
}
}

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

function findLongestWord(arr) {
if(arr.length == 0){
return null;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you return here, you dont need to have an else statement as you are returning here (meaning it will go out of the function and not execute the rest of the function anyways)

} else if(arr.length == 1) {
return arr[0];
} else {
const lengthArr = arr.map(item => item.length);
const longestWord = arr[lengthArr.indexOf(Math.max(...lengthArr))];
return longestWord;
}
}

// Iteration #3: Calculate the sum

const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];

function sumNumbers(arr){
let sum1 = 0;

for(let i=0; i < arr.length; i++){
sum1 += arr[i];
}

return sum1;
}

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

function sum(mixArr){
let sum2 = 0;

for(let j=0; j < mixArr.length; j++) {
if(typeof mixArr[j] == "number") {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can actually make and or statement here:
typeof mixArr[j] == "number" || typeof mixArr[j] == "boolean"

since a number + a boolean results in: 5 + true === 6, and 5 + false === 5

sum2 += mixArr[j];
} else if(typeof mixArr[j] == "string") {
sum2 += mixArr[j].length;
} else if(typeof mixArr[j] == "boolean") {
if(mixArr[j] == true) {
sum2 += 1;
}
} else {
throw new Error("Unsupported data type sir or ma'am");
}
}

return sum2;
}

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

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

return sumNumbers(numArr)/numArr.length;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to see you are re-using your functions!

}

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

function averageWordLength(strArr){
let sum3 = 0;

if(strArr.length == 0){
return null;
}

for(let k=0; k < strArr.length; k++){
sum3 += strArr[k].length;
}

return sum3/strArr.length;
}

function avg(arr){
if(arr.length == 0){
return null;
}

return parseFloat((sum(arr)/arr.length).toFixed(2));
}

// Iteration #5: Unique arrays
const wordsUnique = [
'crab',
Expand All @@ -29,9 +113,29 @@ const wordsUnique = [
'bring'
];

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

return Array.from(new Set(wordsArr));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever;)

}

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

function doesWordExist (wordsArr, searchterm){
if(wordsArr.length == 0){
return null;
}

if(wordsArr.find(word => word == searchterm) !== undefined){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I THINK you can instead write:
return wordsArr.find(word => word == searchterm) !== undefined

this will either return true or false depending if the condition is met or not

return true;
} else {
return false;
}
}

// Iteration #7: Count repetition
const wordsCount = [
'machine',
Expand All @@ -47,6 +151,18 @@ const wordsCount = [
'matter'
];

function howManyTimes(wordsArr, searchterm){
let sum5 = 0;

wordsArr.forEach(function(word){
if(word == searchterm){
sum5 += 1;
}
});

return sum5;
}

// Iteration #8: Bonus

const matrix = [
Expand All @@ -71,3 +187,7 @@ const matrix = [
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
];

function greatestProduct(matrix){
return null;
}