Skip to content

[LIS-WDFT-APR2023] Elnaz #3738

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 1 commit into from
Closed
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
week1day2
  • Loading branch information
elnazfe committed Apr 12, 2023
commit 4dd98a81c79cc6982a24c5047f421ebed84543b5
1 change: 1 addition & 0 deletions lab-javascript-functions-and-arrays
Submodule lab-javascript-functions-and-arrays added at 751ea3
202 changes: 189 additions & 13 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,140 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}
function maxOfTwoNumbers(num1,num2) {
if (num1 > num2) {
return num1;
} else {
return num2;
}
}
console.log(maxOfTwoNumbers(100,200))




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

function findLongestWord() {}
function findLongestWord(words) {
if (words.length === 0) {
return null;
}
else if (words.length === 1) {
return words[0]
}
let longestWord = "";
for (let i = 0; i < words.length; i++) {
if (words[i].length > longestWord.length) {
longestWord = words[i];
}
}
return longestWord;
}




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

function sumNumbers() {}
function sumNumbers(numbers) {
if (numbers.length === 0) {
return 0;
}
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
if (sum === 0) {
return 0;
}

return sum;
}



// Iteration #3.1 Bonus:
function sum() {}
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];

function sum(mixedArr) {
if (mixedArr.length === 0) {
return 0;
}
let totalSum = 0;
for (let i = 0; i < mixedArr.length; i++) {
if (typeof mixedArr[i] === 'number') {
totalSum += mixedArr[i];
} else if (typeof mixedArr[i] === 'string') {
totalSum += mixedArr[i].length;
} else if (typeof mixedArr[i] === 'boolean') {
if (mixedArr[i]) {
totalSum += 1;
}
}
}

return totalSum;
}




// 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) {
if (numbersAvg.length === 0) {
return null;
}
let avg = 0;
let sum = 0;
for (let i = 0; i < numbersAvg.length; i++) {
sum += numbersAvg[i];
avg = sum/numbersAvg.length;
}
return avg;
}

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

function averageWordLength() { }
function averageWordLength(wordsArr) {
if (wordsArr.length === 0) {
return null;
}
else {
let wordsAvg = 0;
let wordsLength = 0;
for (let i = 0; i < wordsArr.length; i++) {
wordsLength += wordsArr[i].length
wordsAvg = wordsLength/wordsArr.length
}
return wordsAvg;
}
}

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

function avg(mixedArr) {
if (mixedArr.length === 0) {
return null;
}
let arrSum = 0;
for (let i = 0; i < mixedArr.length; i++) {
if (typeof mixedArr[i] === 'number') {
arrSum += mixedArr[i];
} else if (typeof mixedArr[i] === 'string') {
arrSum += mixedArr[i].length;
} else if (typeof mixedArr[i] === 'boolean') {
if (mixedArr[i] === true) {
arrSum += 1;
}
arrAvg = arrSum/mixedArr.length
Copy link

Choose a reason for hiding this comment

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

This line of code where you are calculating the average should be outside of any condition, otherwise if for some reason the array passed to the function doesn't have any boolean that line of code will never be read, so you can actually do this math operation in the return:

function avg(mixedArr) {
  if (mixedArr.length === 0) {
    return null;
  } 
  let arrSum = 0;
    for (let i = 0; i < mixedArr.length; i++) {
      if (typeof mixedArr[i] === 'number') {
        arrSum += mixedArr[i];
      } else if (typeof mixedArr[i] === 'string') {
        arrSum += mixedArr[i].length;
      } else if (typeof mixedArr[i] === 'boolean') {
        if (mixedArr[i] === true) {
          arrSum += 1;
        }
      }else{
        throw new Error("Invalid data type")
      }
    }
    return arrSum/mixedArr.length 
  }

Also, you're missing the last test which is to throw an error. For that you just need an else with the syntax for throwing an error inside. The jasmine test will continue to be red but your answer will be correct don't worry.

}
}
return arrAvg;
}

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

function uniquifyArray() {}

function uniquifyArray(wordsUnique) {
if (wordsUnique.length === 0) {
return null;
}
else {
let simpleArr = [];
wordsUnique.forEach((word)=>{
if(simpleArr.includes(word) === false) {
simpleArr.push(word);
}
});
return simpleArr;
}
}


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

function doesWordExist() {}
function doesWordExist(wordsFind, word) {
if (wordsFind.length === 0) {
return null;
}
else {
return wordsFind.includes(word);
}
}



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

function howManyTimes() {}
function howManyTimes(wordsCount, theWord) {
if (wordsCount.length === 0) {
return 0;
}
let count = 0;
for (let i = 0; i < wordsCount.length; i++) {
if (wordsCount[i] === theWord) {
count++;
}
}
return count;
}



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

function greatestProduct() {}

// really hard!!!!!!!!

function greatestProduct(matrix) {
let greatestProduct = 0;

for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length - 3; j++) {
let product = matrix[i][j] * matrix[i][j+1] * matrix[i][j+2] * matrix[i][j+3];
if (product > greatestProduct) {
greatestProduct = product;
}
}
}


for (let i = 0; i < matrix.length - 3; i++) {
for (let j = 0; j < matrix[i].length; j++) {
let product = matrix[i][j] * matrix[i+1][j] * matrix[i+2][j] * matrix[i+3][j];
if (product > greatestProduct) {
greatestProduct = product;
}
}
}


for (let i = 0; i < matrix.length - 3; i++) {
for (let j = 0; j < matrix[i].length - 3; j++) {
let product = matrix[i][j] * matrix[i+1][j+1] * matrix[i+2][j+2] * matrix[i+3][j+3];
if (product > greatestProduct) {
greatestProduct = product;
}
}
}


for (let i = 3; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length - 3; j++) {
let product = matrix[i][j] * matrix[i-1][j+1] * matrix[i-2][j+2] * matrix[i-3][j+3];
if (product > greatestProduct) {
greatestProduct = product;
}
}
}

return greatestProduct;
}




Expand Down