Saturday, April 1, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home JavaScript

Pattern with auto increment in javascript – JavaScript – SitePoint Forums

learningcode_x1mckf by learningcode_x1mckf
September 21, 2022
in JavaScript
0
Time limit for notify – JavaScript – SitePoint Forums
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


You might also like

4 Packages for Working With Date and Time in JavaScript – MUO – MakeUseOf

Understanding the Power of Proxy in JavaScript – hackernoon.com

JavaScript vs. TypeScript: What's the difference? – TheServerSide.com

I’m attempting to create the sample the place the quantity will auto-increment with at the least one prefix with zero when the quantity is over. for instance preliminary worth will likely be ABCD0001 once I add 1 it is going to be ABCD0002 so on it’ll repeatedly work like ABCD9999. now the digit is over then it’ll convert to ABCD010000 means if the digit will over auto the quantity zero add as like right here we will see ABCD010000 I’m attempting to create this perform nevertheless it removes all of the zeroes from the code. please assist how I create this perform which might give the anticipated output within the javascript. I’m attempting to create nevertheless it not working as beneath.

It’s a little bit inconsistent that you’ve ABCD0001, ABCD0002 after which go to ABCD10000, ABCD10001. So should you enhance the variety of digits, you need to begin with ABCD1, ABCD2 and so forth.

However here’s a resolution:

let id = "ABCD9991";

for(let i = 0; i < 100; i++)

    let strings = id.exchange(/[0-9]/g, '');
    let digits = (parseInt(id.exchange(/[^0-9]/g, '')) + 1).toString();
    if(digits.size < 4)
        digits = ("000"+digits)	.substr(-4);
    id = strings + digits;
    console.log(id);


jsfiddle.net

Edit fiddle – JSFiddle – Code Playground

Check your JavaScript, CSS, HTML or CoffeeScript on-line with JSFiddle code editor.

1 Like

Welcome to the discussion board @wawane7256 :slight_smile:

My try

First a perform to get the required padding size of a given string

// strings have a size property '22'.size === 2
perform getPadLength ( size ) 
  if (size < 5) return 4
  // if an odd quantity in size return the size + 1
  return (size % 2 === 0) ? size : size + 1


getPadLength('24') // 4

getPadLength('52367') // 6

getPadLength('102367') // 6

Now a perform to pad with zeros

perform padZeros (num) 
  const numStrg = num.toString()
  const padding = getPadLength(numStrg)

  return numStrg.padStart(padding, '0')


padZeros(24) // '0024'

padZeros(10235) // '010235'

padZeros(102356) // '102356'

[2, 64, 512, 1024, 32768].map(padZeros)

// ['0002', '0064', '0512', '1024', '032768']
developer.mozilla.org

String.prototype.padStart() – JavaScript | MDN

The padStart() methodology pads the
present string with one other string (a number of occasions, if wanted) till the ensuing
string reaches the given size. The padding is utilized from the beginning of the
present string.

thanks in your reply…

perform padZeros (num, mynumber) 
  const numStrg = num.toString() + mynumber;
  const padding = getPadLength(numStrg)
    return numStrg.padStart(padding, '0')

console.log(padZeros(‘ABCD0001’, 1)); // 0ABCD00011
anticipated output: ABCD0002

Thallius:

substr

very good, nevertheless it deprecated as NodeJs saying…

What i gave you simply offers with the numbers. I believed you may have the ability to determine the remainder :slight_smile:

Possibly you could possibly break up the string first. Then be part of afterwards.

wawane7256:

very good, nevertheless it deprecated as NodeJs saying…

Sorry, this occurs should you swap to usually between PHP and JS :slight_smile:
Use substring() as an alternative.

2 Likes



Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

4 Packages for Working With Date and Time in JavaScript – MUO – MakeUseOf

by learningcode_x1mckf
April 1, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

4 Packages for Working With Date and Time in JavaScript  MUO - MakeUseOf Source link

Read more

Understanding the Power of Proxy in JavaScript – hackernoon.com

by learningcode_x1mckf
April 1, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Understanding the Power of Proxy in JavaScript  hackernoon.com Source link

Read more

JavaScript vs. TypeScript: What's the difference? – TheServerSide.com

by learningcode_x1mckf
April 1, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

JavaScript vs. TypeScript: What's the difference?  TheServerSide.com Source link

Read more

JetBrains updates IDEs for Java, JavaScript, Ruby – InfoWorld

by learningcode_x1mckf
March 31, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

JetBrains updates IDEs for Java, JavaScript, Ruby  InfoWorld Source link

Read more

Virtru Announces First Ever FIPS 140-2 Validated JavaScript … – GlobeNewswire

by learningcode_x1mckf
March 30, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Virtru Announces First Ever FIPS 140-2 Validated JavaScript ...  GlobeNewswire Source link

Read more
Next Post
Oracle Rolls Out Java 19

Oracle Rolls Out Java 19

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Azul Vulnerability Detection uncovers known vulnerabilities in Java applications

Azul Vulnerability Detection uncovers known vulnerabilities in Java applications

November 3, 2022
What are checked vs. unchecked exceptions in Java?

What are checked vs. unchecked exceptions in Java?

December 5, 2022
West Java targets apprenticeship for 60 millennial farmers in Japan

West Java targets apprenticeship for 60 millennial farmers in Japan

January 24, 2023

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • So why did they decide to call it Java? – InfoWorld
  • Senior Java Developer – IT-Online
  • 4 Packages for Working With Date and Time in JavaScript – MUO – MakeUseOf

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?