Skip to content

Lab solved arrays #4239

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 4 commits 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ lab-js-functions-and-arrays
```

We will be working with the `src/functions-and-arrays.js`. You can find all the files in the `jasmine` folder needed to use Jasmine. All these files are already linked with the `SpecRunner.html` file.

<>
If you want to check the tests, they are in the `tests/functions-and-arrays.spec.js` file.


Expand Down
160 changes: 138 additions & 22 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,110 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}
function maxOfTwoNumbers(a,b) {
if (a > b) {
return a;
} else if (b > a) {
return b;
} else {
return a;
}
}



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

function findLongestWord() {}
function findLongestWord(word) {
if (word.length === 0) {
return null;
}

let longestWord = word[0];
for (let i = 1; i < word.length; i++) {
if (word[i].length > longestWord.length) {
longestWord = word[i];
}
}
return longestWord;
}



// Iteration #3: Calculate the sum
// Iteration #3: Calculate the sum of array numbers
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
function sumNumbers(numbers) {
if (!Array.isArray(numbers) || numbers.length === 0) return 0; // guard clause

function sumNumbers() {}

let sum = 0; // accumulator
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i]; // add each element
}
return sum; // give back the total

}

// Iteration #3.1 Bonus:
function sum() {}
/*function sum(numbers) {
return numbers.reduce((acc, curr) => acc + curr, 0);*/



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

function averageNumbers() {}
function averageNumbers(numbers) {
if (!numbers || numbers.length === 0) {
return null;
}
const sum = sumNumbers(numbers);
return sum / numbers.length;
}

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

function averageWordLength(words) {
if (!words || words.length === 0) {
return null;
}
const totalLength = words.reduce((acc, curr) => acc + curr.length, 0);
return totalLength / words.length;
}*/


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

function averageWordLength() { }
/**
* Computes the average word length of an array of strings.
* @param {string[]} words – Array of words.
* @returns {number|null} Average length, or null if the array is empty.
*/
function averageWordLength(words = []) {
if (!Array.isArray(words) || words.length === 0) return null; // 1️⃣

// 2️⃣ & 3️⃣ – works for one‑element arrays and larger ones
let total = 0;
for (let i = 0; i < words.length; i++) {
total += words[i].length;
}
return total / words.length;
}

// Quick checks
console.log(averageWordLength([])); // → null
console.log(averageWordLength(['hello'])); // → 5
console.log(averageWordLength(wordsArr)); // → 5.3



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

}

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

function uniquifyArray() {}
function uniquifyArray() {

}



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

function doesWordExist() {}
function doesWordExist(word) {
if (!word || word.length === 0) {
return null;
}




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

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



Expand Down Expand Up @@ -106,10 +192,41 @@ 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) {
let maxProduct = 0;

for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
const current = matrix[i][j];

// Check right
if (j + 3 < matrix[i].length) {
const product = current * matrix[i][j + 1] * matrix[i][j + 2] * matrix[i][j + 3];
maxProduct = Math.max(maxProduct, product);
}

// Check down
if (i + 3 < matrix.length) {
const product = current * matrix[i + 1][j] * matrix[i + 2][j] * matrix[i + 3][j];
maxProduct = Math.max(maxProduct, product);
}

// Check diagonal right
if (i + 3 < matrix.length && j + 3 < matrix[i].length) {
const product = current * matrix[i + 1][j + 1] * matrix[i + 2][j + 2] * matrix[i + 3][j + 3];
maxProduct = Math.max(maxProduct, product);
}

// Check diagonal left
if (i + 3 < matrix.length && j - 3 >= 0) {
const product = current * matrix[i + 1][j - 1] * matrix[i + 2][j - 2] * matrix[i + 3][j - 3];
maxProduct = Math.max(maxProduct, product);
}
}
}

return maxProduct;
}

// The following is required to make unit tests work.
/* Environment setup. Do not modify the below code. */
Expand All @@ -126,5 +243,4 @@ if (typeof module !== 'undefined') {
doesWordExist,
howManyTimes,
greatestProduct
};
}
}
12 changes: 12 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Lab: JavaScript Functions and Arrays</title>
</head>
<body>
<h1>Lab: JavaScript Functions and Arrays</h1>
<p>Abre la consola para ver los resultados de tu código JavaScript.</p>
<script src="functions-and-arrays.js"></script>
</body>
</html>