Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Paris Cécile et Régis #613

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

Conversation

jourisque
Copy link

else {
newArray.push (array[0]);

for (var i=1; i< array.length; i++){

Choose a reason for hiding this comment

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

Using a for loop IN a for loop, is quite risky.. specially when you have a much more simple way to do it.

You can use 2 methods:

indexOf :
array.indexOf("Word") => will return the index of Word in the array if Word exists, or return -1 if the word doesn't exist. If it returns an index, you shouldnt push, if it returns -1 then you should push it.

Includes:
array.includes("Word") => will return TRUE if word exists, or FALSE if it doesnt exist. Push when its false.

example with the indexOf :

for (var i = 0; i < array.length; i++) {
    if (newArray.indexOf(array[i]) === -1) {
      newArray.push(array[i]);
    }
  }

Hope it's clear :)


return temp;
}

var matrix = [
[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0],

Choose a reason for hiding this comment

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

Here is the correction for the Matrix:

function greatestProduct(m) {
  var horizontal, vertical, diagonal;
  var greatProduct = 0;

  for (var i=0, nRows = m.length; i < nRows - 1; i++) {
    for (var j=0, nCols = m[0].length; j < nCols - 1; j++) {
      // 4 horizontal elements
      if (j < nCols-3) {
        horizontal = m[i][j] * m[i][j+1]   * m[i][j+2]   * m[i][j+3];
      }

      // 4 vertical elements
      if (i < nRows-3) {
        vertical   = m[i][j] * m[i+1][j]   * m[i+2][j]   * m[i+3][j];
      }

      // 4 Diagonal elements
      if (i < nRows-3 && j < nCols-3) {
        diagonal   = m[i][j] * m[i+1][j+1] * m[i+2][j+2] * m[i+3][j+3];
      }

      greatProduct = Math.max(greatProduct, horizontal, vertical, diagonal);
    }
  }

  return greatProduct;
}

Choose a reason for hiding this comment

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

It's globally well done! The code is clean and not too long. 💪

Keep up the good work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.