Tic - Tac - Toe… each() not a function

182
March 26, 2018, at 10:34 AM
function whoWonGame(){
const winnerSection = ['row1', 'row2', 'row3','column1', 'column2', 'column3', 'dag1', 'dag2' ];
let winner = null;
winnerSection.each((section) => {
  let match = true;
  for(let i = 1; i <= 2; i++ ){
   $(`${section}`).each((div) => {
      if(div.hasClass(`player${i}`)){
          match = false;
      }
    })
    if(match){
      winner = `player${i}`;
    }
    return winner;
  }

})
}

I get an error saying winnerSection.each is not a function. How could that be?

Answer 1

it's Array.prototype.forEach, not each.

Answer 2

winnerSection is not a jQuery object. To iterate over an array using .each you must do so:

$.each(winnerSection, function(index, section) {
});

See documentation: http://api.jquery.com/jquery.each/

or use the javascript foreach function: https://developer.mozilla.org/ca/docs/Web/JavaScript/Referencia/Objectes_globals/Array/forEach

winnerSection.forEach(function(section, index) {
});

Caution: the order of parameters of both functions.

I write the code corrected as I think, because selectors and strings used are not correct:

$.each(winnerSection, function(index, section) {
  let match = true;
  for(let i = 1; i <= 2; i++ ){
   $(section).each((div) => {
      if(div.hasClass('player'+i)){
          match = false;
      }
    })
    if(match){
      winner = 'player'+i;
    }
    return winner;
  }
});
Answer 3

winnerSection is not extending jQuery. Try with this:

$(winnerSection).each(.......);

Rent Charter Buses Company
READ ALSO
Add link to bootstrap modal caller

Add link to bootstrap modal caller

I have a block that calls a bootstrap modal to show an article when left-clicked, I need to add a link to it so a page can be opened when right clicked and "Open in new window" chosenI'm new to bootstrap and I don't know how to even start searching for it

258
jQuery detect whether an element has been linked to

jQuery detect whether an element has been linked to

I want to attach a jQuery event handler to a <div> element such that whenever a link is clicked which points to this <div>, that handler is activated and the associated function is executed – regardless of the location of the link (same page, other page, other site) pointing...

191
Using BouncyCastle for SHA3-224 Encryption

Using BouncyCastle for SHA3-224 Encryption

I am trying to encrypt a message using BouncyCastle, however I am only getting the correct hash for messages that have a length which is a multiple of 8Is there any way of disabling the padding that happens when the digest method is called?

198
Listen for major location changes only

Listen for major location changes only

I need to listen for major changes that happen in user location based on specific location I have and based on distance then show specific message for userI know i can check the location changes using many ways like implement LocationListener or register...

187