Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

WDFT BER 082020 Gilad Tsabar #1807

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 7 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
139 changes: 139 additions & 0 deletions 139 src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,101 @@
// Iteration #1: Find the maximum
const maxOfTwoNumbers = (first_number, another_number) => {
return Math.max(first_number,another_number);
};

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

const findLongestWord = (words) => {
let longestWordSoFar = '';

for(let word of words){
if(word.length > longestWordSoFar.length) longestWordSoFar = word;
}

return longestWordSoFar || null;
};

// Iteration #3: Calculate the sum

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

const sumNumbers = (numbers) => {
let outcome = 0;

for(let number of numbers){
if(typeof number === 'number') outcome += number;
}

return outcome || 0;
};

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

const sum = (mixedArray) => {
let output = 0;

for(let item of mixedArray){
// switch(typeof item){
// case 'number':
// output+= item;
// break;
// case 'string':
// output+= item.length;
// break;
// case 'boolean':
// output+= item ? 1 : 0;
// break;
// default:
// throw new Error("Unsupported data type sir or ma'am");
// }

let element_type = typeof item;
if (element_type === 'object') throw new Error(`Unsupported data type sir or ma'am`);
if (element_type === 'string') output += item.length;
if (element_type !== 'string') output += item;
}

return output || 0;
};

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

const averageNumbers = (numbers) => {
return sumNumbers(numbers)/numbers.length || null;
};


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

const averageWordLength = (words) => {
// Assuming all items in the array are strings !!!

if(!words || words.length === 0) return null;

const words_length_sum = sum(words);
const words_length_average = words_length_sum / words.length;
return words_length_average;
};

const avg = (items) => {

if(!items || items.length === 0) return null;

const items_length_sum = sum(items);

let items_count = 0;
for(let item of items){
if(typeof item === 'number' || typeof item === 'string' || typeof item === 'boolean') {items_count++;}
}
const items_length_average = items_length_sum / items_count;

return Math.round((items_length_average + Number.EPSILON) * 100) / 100;
};

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

const uniquifyArray = (words) => {

if(!words || words.length === 0) return null;

let unique_words = [];
for(let word of words){
if(unique_words.indexOf(word) < 0) {
unique_words.push(word);
}
}

// let unique_words = [...Set(words)];

return unique_words;
};

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

const doesWordExist = (words, search_word) => {
if(!words || words.length === 0 || !search_word) return null;

for (let word of words) {
if(word === search_word) {
return true;
}
}

// return words.includes(search_word);

return false;
};

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

const howManyTimes = (words, search_word) => {
let counter = 0;
for( let word of words){
if(word === search_word) counter++;
}

// return words.filter(word_filter => word_filter === search_word).length;
return counter;
};

// Iteration #8: Bonus

const matrix = [
Expand All @@ -71,3 +193,20 @@ 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 greatestProductSoFar = 0;
let productHor;
let productVer;

for(let i=0; i<matrix.length; i++){
for(let j=0; j<matrix[i].length-4; j++){
productHor = matrix[i][j] * matrix[i][j+1] * matrix[i][j+2] * matrix[i][j+3];
productVer = matrix[j][i] * matrix[j+1][i] * matrix[j+2][i] * matrix[j+3][i];
greatestProductSoFar=Math.max(productHor, productVer, greatestProductSoFar);
}
}

return greatestProductSoFar;
};
Morty Proxy This is a proxified and sanitized view of the page, visit original site.