Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this matrix`
[
[5,0,0,4],
[0,5,3,2],
[1,0,5,0],
[2,0,4,2]];
I need to check if any row contains 0, then return true, else return false. In this example it must return true, coz in every row there is minimum one 0.

What I have tried:

I have tried like this, but it's wrong.

function F(matrix){
	for(var i = 0; i < matrix.length; i++){
		for(var j = 0; j < matrix.length; j++){
			if(matrix[i][j] == 0){
				return true;
			}
		}
	}
	return false;
}
console.log(F([[5,0,0,4],[0,5,3,2],[1,0,5,0],[2,0,4,2]]));
Posted
Updated 27-Jul-18 1:08am
Comments
dan!sh 27-Jul-18 6:13am    
These are really sounding like homework questions now. Do it yourself or seek help from your teacher. Here is another question from same user: clickety click click
Suren97 27-Jul-18 6:17am    
no it's not homework, i just wanna develop my logic. But what have i written wrong here? I think it must work true :(
dan!sh 27-Jul-18 6:25am    
You can debug the code and identify the problem. Refer to response for your other question too.
Suren97 27-Jul-18 6:31am    
How can i check every row in sequence?
Richard MacCutchan 27-Jul-18 6:49am    
You are still using the number of rows where you should be using the number of columns for your inner loop. Just like I already explained earlier today.

You example code is correct if you want to check if any row contains a zero item. But you probably want to check if every row contains at least one zero item.

Then it can be solved by breaking (returning false) when the required condition is not met and finally returning true (no break occured) and using a variable per row indicating if the current row has one or more items that are zero:
javascrip
for(var i = 0; i < matrix.length; i++){
    var hasZero = false;
    //for(var j = 0; j < matrix.length; j++){
    for(var j = 0; j < matrix[i].length; j++){
        if(matrix[i][j] == 0){
            hasZero = true;
            break;
        }
    }
    // No item in this row is zero
    // So break and return false
    if (!hasZero)
        return false;
}
// All rows contain at least one item that is zero
return true;
 
Share this answer
 
v2
Comments
Suren97 27-Jul-18 6:55am    
Thank you very much
Richard Deeming 27-Jul-18 8:35am    
for(var j = 0; j < matrix.length; j++){

Should really be:
for(var j = 0; j < matrix[i].length; j++){

Just in case the matrix isn't square. :)
Jochen Arndt 27-Jul-18 8:38am    
Thank you for that. I will update my answer.

I realised it meanwhile too but thought he should know due to all the other post mentioning it.
matrix.length is the number of rows in matrix.
Your code works because matrix have same number of columns.
Quote:
I have tried like this, but it's wrong.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
JavaScript Debugging[^]
Chrome DevTools  |  Web  |  Google Developers[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

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



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