Skip to content

[Remote-PT-062020] Debora Crescenzo #1716

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 5 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
237 changes: 237 additions & 0 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,132 @@
// Iteration #1: Find the maximum
const maxOfTwoNumbers = (num1,num2) => Math.max(num1, num2);

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

const findLongestWord = list => {
if (list.length > 0){
let longestWord,
temp,
len = list.length;
for (let i = 0; i < len; i++) {
longestWord = i;
for (let j = i + 1; j < len; j++) {
if (list[j].length > list[longestWord].length) {
longestWord = j;
}
}
temp = list[i];
list[i] = list[longestWord];
list[longestWord] = temp;
}
return list[0];
} else{
return null;
}

};

// Iteration #3: Calculate the sum

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

const sumNumbers = arr => {
let sum = 0
arr.forEach(num =>{
sum += num
})
return sum
};

// Bonus - Iteration #3.1: A generic sum() function

const sum = arr => {
let totSum = 0;
if (!arr.length){
return totSum
}else{
for (let i=0; i < arr.length; i++){
if (typeof (arr[i]) === 'boolean'){
let boolToNum = arr[i] ? 1 : 0;
totSum += boolToNum;

}else if (typeof (arr[i]) === 'string'){
let stringLength = arr[i].length;
totSum += stringLength;

}else if (typeof (arr[i]) === 'number'){
totSum += arr[i];

}else if (typeof (arr[i]) === 'object'){
throw new Error ("Unsupported data type sir or ma'am");

} else {
continue;
}
}
} return totSum;
};

sum(numbers);

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

const averageNumbers = arr =>{
if (!arr.length){
return null;
}else{
let average = sumNumbers(arr)/arr.length;
return average;
}
}
averageNumbers(numbersAvg);

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

const averageWordLength = arr =>{
if (!arr.length){
return null;
}else{
let average = sum(arr)/arr.length;
return average;
}
}
averageWordLength(wordsArr)

// ALTERNATIVE SOLUTION
// const averageWordLength = (arr) =>{
// if (!arr.length){
// return null;
// }else{
// let sum = 0;
// let averageLength = 0;
// arr.forEach(word =>{ sum += word.length })
// averageLength = sum/arr.length;
// return averageLength;
// }
// };
// averageWordLength(wordsArr)


//Bonus - Iteration #4.1: A generic avg() function

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

const avg = arr => {
if (!arr.length){
return null
}else{
let averageOfArray = (sum(arr)/arr.length).toFixed(2);
return parseFloat(averageOfArray)
}
}

avg(mixedArr)

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

const uniquifyArray = (arr) =>{
if (!arr.length){
return null
}else {
let newArray = [];
arr.forEach(word =>{
if (newArray.indexOf(word)<0){
newArray.push(word)
}
})

return newArray;
}
};

//ALTERNATIVE SOLUTION
// const uniquifyArray = arr =>{
// if (!arr.length){
// return null
// }else {
// let result = [];
// let temp = [];
// for (let i = 0; i < arr.length; i++)
// if (!(arr[i] in temp)) {
// temp[arr[i]] = 1; // true = 1
// result.push(arr[i]);
// }
// return result;
// }
// };

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

const doesWordExist = (arr, word) =>{
if (!arr.length){
return null;
}else{
let temp= false;
for (i=0; i < arr.length; i++){
if (word === arr[i]){
temp = true;
}
} return temp;
}
}

console.log(doesWordExist(wordsFind, 'trouble'))



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

const howManyTimes = (arr, word) => {
if (!arr.length){
return 0
}else {
let temp = [];
for (i=0; i<arr.length; i++){
if (word === arr[i]){
temp += 1
}
}return temp.length;
}
};

console.log(howManyTimes(words,'matter' ))

// Iteration #8: Bonus

const matrix = [
Expand All @@ -71,3 +247,64 @@ 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]
];

const greatestProduct = (matrix) => {
let max = 0;
//1. iterate the rows;
for (i=0; i<matrix.length; i++){

//2. iterate the columns;
for (j=0; j<matrix.length; j++){

//3. check the maximum product in horizontal row;
if ((j - 3) >= 0){
result = (matrix[i][j] * matrix[i][j - 1] * matrix[i][j - 2] * matrix[i][j - 3]) //
if (max < result){
max = result }
}
//4. check the maximum product in vertical row;
if ((i - 3) >= 0){
result = (matrix[i][j] * matrix[i-1][j] * matrix[i-2][j] * matrix[i - 3][j]) //
if (max < result){
max = result }
}

} return max
}
}

console.log(greatestProduct(matr))


const greatestProductOfDiagonals = matrix =>{
let max = 0;
//1. iterate the rows;
for (i=0; i<matrix.length; i++){

//2. iterate the columns;
for (j=0; j<matrix.length; j++){

// check the maximum product in diagonal going through down - right
if (((i - 3) >= 0) && ((j - 3) >= 0)){
result = (matrix[i][j] * matrix[i - 1][j - 1] *
matrix[i - 2][j - 2] * matrix[i - 3][j - 3])

if (max < result){
max = result }
}

//check the maximum product in diagonal going through up - right
if (((i - 3) >= 0 ) && ((j - 1) <= 0)){
result = (matrix[i][j] * matrix[i - 1][j + 1] *
matrix[i - 2][j + 2] * matrix[i - 3][j + 3])

if (max < result){
max = result }
}


}return max
}
}

greatestProductOfDiagonals (matr)