Skip to main content
Fred.L
Inspiring
August 5, 2022
Answered

Loop for finding a number by itération

Hi guys,

 

I need some advice about how to handle iteration with JS.

For those of you who know what EAN14 are, they follow a calculation that is a bit complex.

here is a link that explains the thing.

https://www.gs1.org/services/how-calculate-check-digit-manually

 

My problem  is only for the last part of the calculation. How would you handle what follows ?

 

- Let's say I get a number (nb1) (that part is done)

- This number has to be substracted to the nearest number above it (nb2). The result is then a number strictly below 10 but possibly 0.

- The result of the substraction is the number I need (nb3).

Theoretically, (nb2) can be anything from 24 to 216 (usually more about 70 to 130)

 

I think about writting a loop in order to iterate the substraction from 216 to 24 and when the result is below 10, I store the value, and I'm done.

 

But I can't get my head around the way to write it down.

Do you have any advice?

maybe I'm missing something there

 

Thanks

Fred

 

PS Find attached what it's gonna look like. I'll translate it into english for those of you who may have the need of it. 

 

This topic is closed to new replies. Start a new post to keep the conversation going.
m1b
m1bCorrect answer
Community Expert
August 5, 2022

Hi @Fred.L, I've had a quick attempt at it. Let me know how it goes. - Mark

/**
 * Returns the EAN barcode check digit
 * where `num` is the barcode number
 * (without the check digit of course).
 * @test EAN13 number 629104150021 --> returns 3
 * @test EAN14 number 1978236328953 --> returns 4
 * @date 2022-08-06
 * @param {Number|String} num
 * @returns {Number} - the check digit
 */
function getEANCheckDigit(num) {

    var str = String(num).split(''),
        sum = 0,
        d;

    while (d = str.shift()) {

        d = Number(d);

        if (d !== d) // NaN
            return;

        multiplier = (str.length - 1) % 2 ? 3 : 1;
        sum += d * multiplier;

    }

    return (Math.ceil(sum / 10) * 10) - sum;

}

 

6 replies

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
August 5, 2022

Hi @Fred.L, I've had a quick attempt at it. Let me know how it goes. - Mark

/**
 * Returns the EAN barcode check digit
 * where `num` is the barcode number
 * (without the check digit of course).
 * @test EAN13 number 629104150021 --> returns 3
 * @test EAN14 number 1978236328953 --> returns 4
 * @date 2022-08-06
 * @param {Number|String} num
 * @returns {Number} - the check digit
 */
function getEANCheckDigit(num) {

    var str = String(num).split(''),
        sum = 0,
        d;

    while (d = str.shift()) {

        d = Number(d);

        if (d !== d) // NaN
            return;

        multiplier = (str.length - 1) % 2 ? 3 : 1;
        sum += d * multiplier;

    }

    return (Math.ceil(sum / 10) * 10) - sum;

}

 

Translate
Marc Autret
Legend
August 5, 2022

Hi Fred,

 

It looks like you're looking after:

 

nb3 = 10-(nb1%10||10)

 

Best,

Marc

Translate
m1b
Community Expert
Community Expert
August 5, 2022

@Marc Autret as always, while I plod my way through the problem, you get right to the point! 🙂

- Mark

Translate