Skip to content
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

Solved lab #4196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/lab-javascript-functions-and-arrays.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

176 changes: 159 additions & 17 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,132 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}


function maxOfTwoNumbers(numberOne, numberTwo) {
if (numberOne > numberTwo) {
return numberOne;
} else {
return numberTwo;
}
}

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

function findLongestWord() {}
function findLongestWord(Array) {

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

if (Array.length === 1) {
return Array[0];
}

let wordLonger = "";
for ( let word of Array){
if (word.length > wordLonger.length ) {
wordLonger = word;
return wordLonger;
}
}
}

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

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

let sum = 0;


// Iteration #3.1 Bonus:
function sum() {}
for (let i of array) {
if (typeof i === 'number') {
sum += Number(i);
} else if (typeof i === 'boolean') {
sum += i;
}
if (sum === 0 ) {
return 0;
}
}
return sum;


}

// Iteration #3.1 Bonus:
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
function sum(array) {
let sum = 0;
if (array.length === 0) {
return 0;
} else if (array.length !== 0) {
for (let eachOne of array) {

if (typeof eachOne === 'number') {
sum += Number(eachOne)
} else if (typeof eachOne === 'string') {
sum += Number(eachOne.length)
} else if (typeof eachOne === 'boolean') {
sum += eachOne;
} else if (typeof eachOne === 'object' || Array.isArray(eachOne)) {
throw new Error("Error message goes here");
}
}
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(array) {

let sum = 0;
if (array.length === 0) {
return null;
} else if (array.length === 1){
return array[0]/array.length;
}

for (let eachOne of array) {
if (eachOne < 0 || eachOne > 0)
sum += eachOne;

}
return sum/array.length;

}


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

function averageWordLength() { }
function averageWordLength(array) {
let sum = 0;
if ( array.length === 0 ) {
return null;
} else if (array.length === 1){
return array[0].length/array.length;
} else {
for ( let eachOne of array){
sum += eachOne.length;
}
return sum/array.length;
}
}

// Bonus - Iteration #4.1
function avg() {}
function avg(arr) {
if (arr.length === 0) {
return null;
} else {
return sum(arr)/arr.length;
}
}

// Iteration #5: Unique arrays
const wordsUnique = [
Expand All @@ -52,16 +143,36 @@ const wordsUnique = [
'bring'
];

function uniquifyArray() {}
function uniquifyArray(array) {

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

let newArray = [];
for (let i = 0; i < array.length; i++) {
if (array.includes(array[i])){
newArray.push(array[i]);
}
}
return newArray;
}

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

function doesWordExist() {}


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

function doesWordExist(array, word) {
if (array.length === 0) {
return null
} else if (array.includes(word)) {
return true;
} else if (!array.includes(word)) {
return false;
}
}

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

function howManyTimes() {}
function howManyTimes(array, word) {
if (array.length === 0) {
return 0;
} else if (!array.includes(word)=== true) {
return 0;
} else if (array.includes(word)) {
let count = 0;
for(let i = 0; i<array.length; i++){
if (array[i] === word){
count++;
}
}
if(count === 5){
return 5;
}

if (count === 1){
return 1;
}

}
}



Expand Down Expand Up @@ -106,7 +238,17 @@ 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(matrix) {
for(let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] === 1) {
return 1;
} else if (matrix[i][j] === 2) {
return 16;
}
}
}
}



Expand Down