Martyboy
Martyboy

Reputation: 370

Function/Object's to shorten code

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.

Upvotes: 1

Views: 101

Answers (2)

kapex
kapex

Reputation: 29959

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.

Upvotes: 1

scotty
scotty

Reputation: 559

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);
    });

Upvotes: 1

Related Questions