JavaScript Issues with breaking a loop legal issues with breaking a lease

So I'm trying to create a program that iterates through 2D array until it finds a matching ticket; however when the program finds the number within the array it doesn't break:

    var namesArray = ["Reece", "Jack", "Lewis", "Ryan"];

    var raffleArray = [
      [193, 109, 97, 479],
      [489, 046, 387, 830],
      [475, 947, 294, 590],
      [330, 395, 750, 649]
    ];

    //function getRandomNum(){
    //  return Math,floor(Math.random() * (999 - ))
    //}

    //loops through the 2D array to search for 
    for (j = 0; j < 5; j++) {
      for (i = 0; i < raffleArray[j].length; i++) {
        if (raffleArray[j][i] == 294) { //294 is only set for testing
          alert(namesArray[j] + " has won the raffle with ticket number:         " + raffleArray[j][i]);
          break;
        } else {
          alert("Ticket: " + raffleArray[j][i]+ " did not win"} //only for testing
      }
    }}

Hope someone can help!

Answer:1

You need to label the outer loop to break out of both.

  search:for (j = 0; j < 4; j++) {
    for (i = 0; i < raffleArray[j].length; i++) {
      if (raffleArray[j][i] == 294) { //294 is only set for testing
        alert(namesArray[j] + " has won the raffle with ticket number:         " + raffleArray[j][i]);
        break search;
      }
  }}
Answer:2

Take a boolean variable to find the required element is matched or not. once it is found break inner loop. where you have to assign true to boolean variable. if boolean var is true then break outer loop.

var isFound = false;

   //loops through the 2D array to search for 
    for (j = 0; j < 5; j++) {
      for (i = 0; i < raffleArray[j].length; i++) {
        if (raffleArray[j][i] == 294) { //294 is only set for testing
            found = true;
          alert(namesArray[j] + " has won the raffle with ticket number:         " + raffleArray[j][i]);
          break;
        } else {
          alert("Ticket: " + raffleArray[j][i]+ " did not win"} //only for testing
      }
      if(isFound) {
        break;
      }
    }}
Answer:3

You can use a temporary variable to break the outer loop as:

    var found = false;

   //loops through the 2D array to search for 
    for (j = 0; j < 5; j++) {
      for (i = 0; i < raffleArray[j].length; i++) {
        if (raffleArray[j][i] == 294) { //294 is only set for testing
            found = true;
          alert(namesArray[j] + " has won the raffle with ticket number:         " + raffleArray[j][i]);
          break;
        } else {
          alert("Ticket: " + raffleArray[j][i]+ " did not win"} //only for testing
      }
      if(found) {
        break;
      }
    }}
Answer:4

Consider the following code: angular.module('app', ['app.core']) .factory('NameService', function() { return { getName: function() { return 'Omar' ;} }; })...

Consider the following code: angular.module('app', ['app.core']) .factory('NameService', function() { return { getName: function() { return 'Omar' ;} }; })...

When using Chartjs with Polymer with setting as dom: 'shadow', I am getting an error as Uncaught TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element But ...

When using Chartjs with Polymer with setting as dom: 'shadow', I am getting an error as Uncaught TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element But ...

I'm trying to attach string with propery binding for example my object is { "name": "The Walking Dead", "imageUrl": "/title/tt1520211/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=2240084122&pf_rd_r=...

I'm trying to attach string with propery binding for example my object is { "name": "The Walking Dead", "imageUrl": "/title/tt1520211/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=2240084122&pf_rd_r=...

My array looks like this: [ {name:'John',IsManager:true}, {name:'Terry'}, {name:'Sam'}, {name:'Andy'} ] I'm trying to get it to display all the people that don't have the IsManager property: ...

My array looks like this: [ {name:'John',IsManager:true}, {name:'Terry'}, {name:'Sam'}, {name:'Andy'} ] I'm trying to get it to display all the people that don't have the IsManager property: ...

  1. angularjs filtering ng-repeat
  2. angularjs ng repeat filter
  3. angularjs ng repeat filter function
  4. angularjs ng-repeat filter by property
  5. angularjs ng-repeat filter multiple fields
  6. angularjs ng repeat filter or condition
  7. angularjs ng-repeat filter not equal
  8. angularjs ng-repeat filter multiple values
  9. angularjs ng-repeat filter by property value
  10. angularjs ng-repeat filter empty
  11. angularjs ng repeat filter conditional
  12. angularjs ng-repeat filter object property
  13. angularjs ng-repeat filter null value
  14. angularjs ng-repeat filter multiple parameters
  15. angularjs ng-repeat filter function parameter
  16. angularjs ng-repeat filter count
  17. angularjs ng-repeat filter array of objects
  18. angularjs ng-repeat filter length
  19. angularjs ng-repeat filter exact match
  20. angularjs ng-repeat filter example