JavaScript When number reaches every nth value inside nested loop

Purpose: A user can choose a Product Type and variation of that Product Type. I need to track every Product Type added and how many variations were added. For Example,
item 1: Shirt > Long Sleeve (1)
item 2: Shirt > V neck (3)
item 3: Pants > Jeans (1)

The total amount pertains to the variation of the product. I'd like to have something happen whenever a user selects 4th variation of the same type. For this example, I want something to happen to the Product type Shirt, as currently long sleeve + v neck = 4

I have two nested loops. First one is looping through each product type, and inner loop should loop through each variation and get the sum.

This is a good starting point but I can't seem to get passed the for loop to check for every nth value of the total # of variations.

jQuery('.product').each(function(i, objP){
    var sum = 0,
    min = 4,
    max = 5;        
    jQuery(objP).find('.product-variation-quantity').each(function(ii, objC){
        sum += parseInt(jQuery(objC).text());
        return sum;
        for ( var x = 0; x < min * max; x += min) {
            if ( sum == x ) {
                var sum_id = jQuery(objP).attr('id');
                console.log(sum_id);
                //product quantity has reached min to pass
            }
            else {
                //has not reached min
            }
        }
    });
});

Any help? note: objP and objC are used to track the contextual this
note2: to clarify: on every 4th, 8th, 12th etc value, something will happen, not every 4th, 5th, 6th

Answer:1

First of all, you have a bug in your code, it's where you return sum; in the nested loop. The for loop after this will never execute.

I suggest this code:

jQuery('.product').each(function(i, objP){
    var sum = 0,
    min = 4,
    max = 5;   
    var ind = 1; 
    jQuery(objP).find('.product-variation-quantity').each(function(ii, objC){
        sum = parseInt(jQuery(objC).text());
        if (sum > 0 && sum % 4 == 0) {
            // Do something
        }

    });
});

Here's codepen sample.

Answer:2

I have read another question on stackoverflow, he said: A closure is one way of supporting first-class functions; it is an expression that can reference variables within its scope (when it was ...

I have read another question on stackoverflow, he said: A closure is one way of supporting first-class functions; it is an expression that can reference variables within its scope (when it was ...

  1. javascript closures definition

I use cloud 9 platform. I have php file with several functions call phpFunctions.php: <?php $servername = "127.0.0.1"; $username = "oshrat"; $password = ""; $database = "myDB"; $dbport = 3306;...

I use cloud 9 platform. I have php file with several functions call phpFunctions.php: <?php $servername = "127.0.0.1"; $username = "oshrat"; $password = ""; $database = "myDB"; $dbport = 3306;...

  1. function from script powershell
  2. call function from script matlab
  3. import function from script python
  4. call function from script
  5. call function from script html
  6. call function from script powershell
  7. function from another script unity
  8. call function from script tag
  9. import function from script powershell
  10. call function from script python
  11. run function from script matlab
  12. call function from script bash
  13. run function from script powershell
  14. matlab function from script
  15. bash function from script
  16. powershell function from scriptblock
  17. python function from script
  18. call function from scriptblock powershell
  19. call function from script tag javascript
  20. javascript function from script

For example I have a function Question.prototype.checkAnswer = function(answer1, answer2) { if (answer1.innerHTML == this.correctAnswer) { console.log("correct"); players1.moveCharacter =...

For example I have a function Question.prototype.checkAnswer = function(answer1, answer2) { if (answer1.innerHTML == this.correctAnswer) { console.log("correct"); players1.moveCharacter =...

  1. execute function only once javascript
  2. execute function only once jquery
  3. execute function only once
  4. execute function only once c#
  5. execute function only once python
  6. execute function only once php
  7. js execute function only once
  8. javascript execute function only one time
  9. angular execute function only once
  10. typescript execute function only once
  11. scala execute function only once
  12. jquery execute function only one time

Say I have an express route that takes numbers, adds them all up and returns the total. Normally I would do something like this app.get('/add/:num1/:num2/:num3', (req, res) => { // access and ...

Say I have an express route that takes numbers, adds them all up and returns the total. Normally I would do something like this app.get('/add/:num1/:num2/:num3', (req, res) => { // access and ...