Skip to content

[FTRMT052023] Solideo #3804

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
141 changes: 130 additions & 11 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,117 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}
function maxOfTwoNumbers(a, b) {
if (a > b) {
return a;
} else if (b > a) {
return b;
} else if (a === b) {
return a || b;
}
}



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

function findLongestWord() {}
function findLongestWord(x) {

Choose a reason for hiding this comment

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

Try not use x, y, a as variables, try to use name that will help you remember what are the arguments that you need to pass when reusing the function

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

let longest = [];
for(let i = 0; i < x.length; i++) {
if(x[i].length > longest.length) {
longest = x[i];
}
}
return longest;
}




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

function sumNumbers() {}
function sumNumbers(y) {
let result = 0;

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

return result;
}



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

let sum = 0;
for(let value of x) {
switch(typeof value) {
case "number":
sum += value;
break;
case "boolean":
if (value) { sum++; }
break;
case "string":
sum+=value.length;
break;
case "object":
throw new Error("Unsupported data type sir or ma'am");
}
}

return 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(a) {

if (a.length === 0) {
return null;
}
return sumNumbers(a) / a.length;

}


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

function averageWordLength() { }
function averageWordLength(array) {

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

return sumNumbers(array.map(x => x.length)) / array.length;

}

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

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

return sum(average) / average.length;

}

// Iteration #5: Unique arrays
const wordsUnique = [
Expand All @@ -52,14 +128,37 @@ const wordsUnique = [
'bring'
];

function uniquifyArray() {}
function uniquifyArray(yyxx) {
const result = [];
if (yyxx.length === 0) {
return null;
}

for(let a of yyxx) {
if(!result.includes(a)) {
result.push(a);
}
}
return result;
}



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

function doesWordExist() {}
function doesWordExist(target, check) {

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


if (target.includes(check)) {
return true;
} else {return false;}

}



Expand All @@ -78,7 +177,20 @@ const wordsCount = [
'matter'
];

function howManyTimes() {}
function howManyTimes(target, phrase) {
if(target.length === 0) {
return 0;
}

let count = 0;
target.forEach(a => {
if( a === phrase) {
count++;
}
})

return count;
}



Expand Down Expand Up @@ -106,7 +218,14 @@ const matrix = [
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
];

function greatestProduct() {}
function greatestProduct(a) {
const flat = a.flat();
const max = Math.max(...flat)
console.log(max)

return max;

}



Expand Down