1

I'd like to shorten some of my code that has different ways of replacing specific patterns that I've set. Basically this code replaces the HTML that I've set as: <span class="combination">0000-AAAA-0000-BBBB</span>

function randomised(len) {
    return Math.floor(Math.random() * len);
}

function randomiseStrings(str){
    var alphSet = "abcdefghijklmnopqrstuvwxyz";
    var numSet = "0123456789";
    var alphNumSet = alphSet.concat(numSet);

    // 1) Replace 0 with random number.
    var str = str.replace(/0/g, function() {
        return numSet[randomised(numSet.length)];
    });

    // 2) Replace A with random number or letter.
    var str = str.replace(/A/g, function() {
        return alphNumSet[randomised(alphNumSet.length)].toUpperCase();
    });

    // 3) Replace B with random letter.
    var str = str.replace(/B/g, function() {
        return alphSet[randomised(alphSet.length)].toUpperCase();
    });

    return str;
}

$('.combination').text(function(i,t){
    return randomiseStrings(t);
});

So as you can see I got 3 identical scripts. However I just couldn't figure out how to do it. What I'm aiming to do is be able to to change these values: str.replace(/[A]/g,, a = alphSet/numSet/alphNumSet and possability to add: .toUpperCase();.

The problem I ended up with what I have no clue how to return these values if I make it as a function. For any suggestion or idea I'd be very thankful.

3
  • 1
    Tip /[0]/ == /0/ and /[A]/ == /A/, etc. You don't need character sets for just one character. Commented Sep 22, 2013 at 0:25
  • 1
    You can just use return numSet[randomised(numSet.length)]; instead of creating a variable. Also define str only once, not three times. Unless you are going to add much more similiar functions, I would leave it as it is now. Alternative solutions using a generic function won't be as clear as it is now. Commented Sep 22, 2013 at 0:28
  • @Christphe, I'm trying to make more "scalable" script just to learn different techniques. So having the option to make custom patterns such as row 3 of the serial code is only numbers 0-2 and letters A,B,C. So for example 0000-0000-0A2C. I just think it's somewhat fun way for me to learn how to do it. Commented Sep 22, 2013 at 0:33

2 Answers 2

1

There are a few places fat can be cut here. First I would go ahead and capitalize all of alphSet so that you don't have to call toUpperCase(). alphNumSet is made up of 2 strings, you can just use string concatenation to combine them rather than the concat function. The function your looking for would just be a matter of factoring out the difference between the calls you're sending.

    function randomised(len) {
        return Math.floor(Math.random() * len);
    }

    function getRandom(str, regEx, set){
        return str.replace(regEx, function() {
            return set[randomised(set.length)];
        });
    }

    function randomiseStrings(str){
        var alphSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        var numSet = "0123456789";
        var alphNumSet = alphSet + numSet;

        str = getRandom(str, /0/g, numSet);
        str = getRandom(str, /A/g, alphNumSet)
        str = getRandom(str, /B/g, alphSet)

        return str;
    }

    $('.combination').text(function(i,t){
        return randomiseStrings(t);
    });
Sign up to request clarification or add additional context in comments.

Comments

1

You already took the first step and identified repeating parts of your code. All you have to do now is to create a function using those parts as parameters.

function replaceWithRandom(text, regex, randomSet) {
  return text.replace(regex, function() {
    return randomSet[randomised(randomSet.length)].toUpperCase();
  });
}

str = replaceWithRandom(str, /0/g, numSet);
str = replaceWithRandom(str, /A/g, alphSet);
str = replaceWithRandom(str, /B/g, alphNumSet);

numSet contains only strings, so calling toUpperCase is superfluous but won't cause any issues.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.