15,615,583 members
0.00/5 (No votes)
See more:
Having problems. X(

I can seem to only get the answer for either one of the problem sets or the other with this code but not both. I assume the problem is with my height/width constants but I think I've been staring at the problem too long.

Going to take the dog for a walk and come back to it.

Need to do this without the help of built in functions.
I already have MaxNumInColumns function completed.
So I just need to compare that array and find the similar number within the array from the function MinNumInRows(below) and then I get my correct answer for the Lucky Number Function.

What I have tried:

```function minNumsInRows(matrix) {
const height = matrix.length;
const width = matrix.length;
const minColumns = [];

for (let row = 0; row < width; row++) {
let rowMin = matrix[row][0];

for ( let col = 0; col < height+1; col++) {

if (rowMin < matrix[row][col]) {
console.log(rowMin, "Compared to: ", matrix[row][col])
console.log("Row Num: ", row)
rowMin = rowMin;

} else {
rowMin = matrix[row][col]
}

} minColumns.push(rowMin)
}
console.log("End")
return minColumns;
};

matrix = [[ 5,  9, 21],
[ 9, 19,  6],
[12, 14, 15]]

console.log(minNumsInRows(matrix)); // [12]

matrix = [[ 5, 10,  8,  6],
[10,  2,  7,  9],
[21, 15, 19, 10]]

console.log(minNumsInRows(matrix)); // [10]```

it works for the first but not the second. First is 3 x 3 so I understand why it works.

However, if I change the width const to:

`const width = matrix[0].length;`

I get an undefined error where I assumed it would give me a value of 4. :(

I can tweak the function to work for ONE or the OTHER, but not both.

Time to take the pup for a walk and hit the procelain throne. :)

Much appreciated for the help AND explaination(I don't want JUST an answer, please.) <3
Posted
Updated 1-Oct-22 19:05pm
v2

## Solution 1

I got it to work and I swore I tried this already. X(

```function MinInRow(matrix) {
const height = matrix[0].length;
const width = matrix.length;

console.log("Height: " + height + " Width: " + width)

const minColumns = [];

for (let row = 0; row < width; row++) {
let rowMin = matrix[row][0];

for ( let col = 0; col < height; col++) {

if (rowMin < matrix[row][col]) {
console.log(rowMin, "Compared to: ", matrix[row][col])
console.log("Row Num: ", row)
console.log("col Num: ", col)
rowMin = rowMin;

}

else {
rowMin = matrix[row][col]

}

}
minColumns.push(rowMin)
console.log(minColumns)
}
console.log("End")
return minColumns;
};   ```

The Whole Lucky Numbers Kit and Kaboodle::

```function luckyNumbers(matrix) {
let luckyNum = [];

let ArrayColumns = maxInColumns(matrix);
let ArrayRows = minInRows(matrix);

for (let i = 0; i < matrix.length; i++) {
if(ArrayColumns.includes(ArrayRows[i])) {
return ArrayRows[i];
}
}

};

function maxInColumns(matrix) {
const height = matrix.length;
const width = matrix[0].length;

const maxColumns = [];

for (let col = 0; col < width; col++){
let colMax = matrix[0][col];

for (let row = 1; row < height; row++) {

if(matrix[row][col] > colMax) {
colMax = matrix[row][col];
}
}
maxColumns.push(colMax);
}
return maxColumns;
};

function minInRows(matrix) {
const height = matrix[0].length;
const width = matrix.length;

const minColumns = [];

for (let row = 0; row < width; row++) {
let rowMin = matrix[row][0];

for ( let col = 0; col < height; col++) {

if (rowMin < matrix[row][col]) {
rowMin = rowMin;

}
else {
rowMin = matrix[row][col]
}
}
minColumns.push(rowMin)
}

return minColumns;
};

matrix = [[ 5,  9, 21],
[ 9, 19,  6],
[12, 14, 15]]

console.log(luckyNumbers(matrix)); // [12]

matrix = [[ 5, 10,  8,  6],
[10,  2,  7,  9],
[21, 15, 19, 10]]

console.log(luckyNumbers(matrix)); // [10]```

v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 160 Richard MacCutchan 115 Dave Kreskowiak 40 Rick York 30 Richard Deeming 30
 OriginalGriff 3,126 Richard MacCutchan 1,381 Graeme_Grant 965 Dave Kreskowiak 689 CHill60 385

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900