Skip to content

MX WDFT 220322 Melissa Osorio #2878

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

Closed
wants to merge 2 commits into from
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
109 changes: 100 additions & 9 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,98 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}
function maxOfTwoNumbers(num1,num2) {
return Math.max(num1,num2);
}

console.log(maxOfTwoNumbers(3,6));


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

function findLongestWord() {}
function findLongestWord(words) {
let maxWord=[];
words.forEach(function(long){
maxWord.push(long.length)
})
let theBiggestNumber = Math.max.apply(null, maxWord);//9
let theBiggestNumerPosition= maxWord.indexOf(theBiggestNumber);
return `The longest word is: ${words[theBiggestNumerPosition]}`;
}
console.log(findLongestWord(words));



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

function sumNumbers() {}
function sumNumbers(numbers) {
let total = 0;
for(let i in numbers){
total += numbers[i];
}
return total;

}

console.log(sumNumbers(numbers));



// Iteration #3.1 Bonus:
function sum() {}
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
function sum(mixedArr) {
let arrayToNum=[];
mixedArr.forEach(function(ifString){
if(typeof(ifString)=="string"){
arrayToNum.push(ifString.length);
}else if(typeof(ifString)=="boolean"){
let booleanToNumber = ifString ? 1 : 0;
arrayToNum.push(booleanToNumber);
}else if(typeof(ifString)=="number"){
arrayToNum.push(ifString);
}
})
let totalSum = 0;
for (let i in arrayToNum){
totalSum += arrayToNum[i];
}
return totalSum;
}

console.log(sum(mixedArr));



// 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) {

let averageNum = sumNumbers(numbersAvg)/numbersAvg.length;
return averageNum;
}

console.log(averageNumbers(numbersAvg));


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

function averageWordLength() { }
//[4, 10, 5, 5, 4, 5, 5, 5, 4, 6]

function averageWordLength(wordsArr) {

let eachWordLength = [];
wordsArr.forEach(function(wordLong){
eachWordLength.push(wordLong.length)
})
return averageNumbers(eachWordLength);

}

console.log(averageWordLength(wordsArr));


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

function uniquifyArray() {}
function uniquifyArray(wordsUnique) {

let uniqueWords = [];

wordsUnique.forEach(function(thing){
if(!uniqueWords.includes(thing)){
uniqueWords.push(thing);
}
})
return uniqueWords;
}

console.log(uniquifyArray(wordsUnique));



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

function doesWordExist() {}
function doesWordExist(wordsFind, wordToFind) {

wordsFind.forEach(function(find){
if(wordsFind.includes(wordToFind)){
console.log(true);
}else{
console.log(false);
}
})

}
doesWordExist(wordsFind, 'machine');


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

function howManyTimes() {}
function howManyTimes(wordsCount, searchWord) {
let totalApear=0;

for(let c of wordsCount){
if(searchWord===c){
totalApear+=1;
}
} console.log(totalApear);

}

howManyTimes(wordsCount, 'matter');

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