## Adobe Support Community

• Global community
• Language:
• Deutsch
• English
• Español
• Français
• Português
• 日本語コミュニティ
Dedicated community for Japanese speakers
• 한국 커뮤니티
Dedicated community for Korean speakers
• Loop for finding a number by itération

# Loop for finding a number by itération

Participant ,
Aug 05, 2022 Aug 05, 2022

Copy link to clipboard

Copied

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.

TOPICS
Scripting

Views

124

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

## 2 Correct answers

Guide , Aug 05, 2022 Aug 05, 2022

Hi Fred,

It looks like you're looking after:

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

Best,

Marc

Likes

Community Expert , Aug 05, 2022 Aug 05, 2022

Hi @-Dain-, 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``````
...

Likes

6 Replies 6
Guide ,
Aug 05, 2022 Aug 05, 2022

Copy link to clipboard

Copied

Hi Fred,

It looks like you're looking after:

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

Best,

Marc

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Expert ,
Aug 05, 2022 Aug 05, 2022

Copy link to clipboard

Copied

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

- Mark

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Participant ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

Thank you both of you for the help and the lesson of scripting.

Both solutions are obviously working ^^

I also found a way to achieve the "same result" myself but with brutal, ugly, amateur scripting which spanned about 22 lines (if … true …then blah blah…else…blah blah), which would probably get blood tears from your eyes if you saw them ^^.

Seeing Marc's beautiful single line of code, achieving the same thing as mine's 22 ugly ones, quite reveals there is much room of improvement for me, and also acts like a punch in the stomach. Damn! From 22 to one single line.

Scripting is definitely a matter of logical approach, certainly experience, making the code as short and concise as possible, which will certainly take some time for me.

Anyways,

Thank you again and see you around. I've got some more ugly code to transform into gold.

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Expert ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

Hi @-Dain-, yes it can be demoralising sometimes, but don't be discouraged—keep going. You have taken on a cool project and that's awesome.

Also, for your learning, I wanted to comment:

@-Dain- wrote:

Scripting is definitely a matter of logical approach, certainly experience, making the code as short and concise as possible

I try hard to write code that is first and foremost understandable, with a strong preference for simplicity. Whether it is concise is a matter for afterwards, and I do not make a function more concise at the expense of understandability and simplicity. It is quite possible that a 22-line function was better—in my view at least—than a clever one-liner, *if* it has those properties of understandability and simplicity. Making code simpler will often make it shorter, but not always, for example, appropriate use of a look-up table will usually make the code longer, but also make it simpler and more understandable!

Note: I am certainly NOT disparaging Marc's answer as a "clever one-liner", it is simple and perfectly understandable. Besides, my answer was a one-liner, too, and interesting that we used different numerical operations to get the same result:

``10 - (nb1 % 10 || 10);``

does the same as:

``Math.ceil(nb1 / 10) * 10 - nb1;``

Anyway, I have often made the mistake of trying to be more clever and more concise for it's own sake and just wanted to give some food for thought. All the best with your project.

- Mark

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Participant ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

LATEST

Thank you very much for explaining your way of thinking, not only it broadens my scope of scripting but helps me feel less ashamed about the code I wrote. ^^

One thing I've learned early on is the importance of comments within the code, not only it acts as a landmark but also explains each step of the logical approach of the script. Very useful on the long run.

Functions are definitely easier to decipher for me than single line code. I believe they are very versatile and understand that it is of much importance to wrap my head around the structure of them.

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Expert ,
Aug 05, 2022 Aug 05, 2022

Copy link to clipboard

Copied

Hi @-Dain-, 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;

}``````

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Resources
Learn and Support
Resources
Crash and Slow Performance