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
120 changes: 98 additions & 22 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,79 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}
function maxOfTwoNumbers(a, b) {
if(a > b) {
return a
} else {
return b }
}
maxOfTwoNumbers(3, 5);



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

function findLongestWord() {}


const words = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];

function findLongestWord(arrayOfWords) {
let longestWord = arrayOfWords[0];
for (let i=0; i<arrayOfWords.length; i++) {
if (arrayOfWords[i].length > longestWord.length) {
longestWord = arrayOfWords[i]
}
}
return longestWord;
}

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

function sumNumbers() {}

function sumNumbers(numbers) {
if(myArray == []) {
result === 0
}
let result = 0;
for(let i = 0; i<numbers.length; i++) {
console.log(numbers[i]);
result = result + numbers[i];
}
return result;
}
console.log(sumNumbers(myArray));


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



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

function averageNumbers() {}
function averageNumbers(_newinput) {
if(_newinput == 0) {
return null;
} else if (_newinput.length == 1) {
return _newinput[0];
} else {
let total = 0;
for (let i = 0; i < numbersAvg.length; i++) {
total += numbersAvg[i];
}
}
let avg = total / 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 == 0) {
return null;
} else if (wordsArr.length == 1) {
return wordsArr[0];
} else {
}
let wordsAvg = wordsArr.join("").length / wordsArr.length;
return wordsAvg;
}

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

function uniquifyArray() {}

function uniquifyArray(arr) {
let newWordArray = [];
if (arr.length === 0) {
return null;
}
for(let i=0; i<arr.length; i++) {
if(newWordArray.indexOf(arr[i]) === -1) { //As a rule,tThe first index of the element in the array; -1 if not found. We use this
//insight to evaluate, as we loop through, that we do not take another element in that is already present in NewWordArray
newWordArray.push(arr[i]); //Pushing it in the new array if it fulfills the IF statement
}
}
return newWordArray;
}
//Approach
//loop in wordsUnique
//push into new array if it's not already there
//indexOf in the new array is -1


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

function doesWordExist() {}

function doesWordExist(wordsFind) {
let newWordArr = [];
if (wordsFind.length === 0) {
return null;
}
for(let i=0; i<wordsFind.length; i++) {
if (wordsFind[i] == newWordArr[i]) {
return false;
} else {
return true;
}
}
console.log(doesWordExist(wordsFind));
}


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

function howManyTimes() {}


function howManyTimes(timesArr) {
let count = [];
for(let i=0; i<timesArr.length; i++) {
if(timesArr.indexOf(count[i]) === -1) {
count.push(timesArr[i]);
}
}
console.log(count);
}


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