Can anyone help me to understand the working of js code ,not able to get it

Here I re-wrote this code in a regular style hopefully you can understand.

Please never write ‘clever’ code (if I can even call it that) like in your example.

An explanation for the original code:

The randColor function returns a string, ‘#’ that is concatenated to the return value of the immediately invoked function expression that follows it.

The IIFE is a recursive function that concatenates to its lor parameter a new random string on each iteration from the given array until that lor parameter reaches the length of 6.

1 Like

It’s a very simple and clever recursion, but rather cryptic approach of doing it. Try to rewrite and separate certain components if you wish.

Something like this

function getRandomHexLetter() {
    const hexLetters = [0,
                        1,
                        2,
                        3,
                        4,
                        5,
                        6,
                        7,
                        8,
                        9,
                        'a',
                        'b',
                        'c',
                        'd',
                        'e',
                        'f'];
    const i = Math.floor(Math.random() * 16);

    return hexLetters[i];
}


function getRandomColor(colorHexString) {
    if (colorHexString.length === 6) {
        return colorHexString;
    }

    return getRandomColor(colorHexString + getRandomHexLetter());
}

function getAsCSSHexString(colorResult) {
    return '#' + getRandomColor(colorResult);
}

getAsCSSHexString(getRandomColor('')); // "#d74002" as an example, truly random
getAsCSSHexString(getRandomColor('d7')); // "#d70bb4" always start with #d7 prefix, rest is random
getAsCSSHexString(getRandomColor('f76')); // "#f768ace" always start with #f76 prefix, rest is random

Better, although more verbose than the original code.

1 Like

I’m not sure about possible expansion stack however (plus with how Javascript handle string and number together adds to confusion (concatenate vs sum addition) -

Hypothetical stack expansions

getRandomColor('');

getRandomColor('' + getRandomColor('d'));
getRandomColor('' + getRandomColor('d' + getRandomColor(7)));
getRandomColor('' + getRandomColor('d' + getRandomColor(7 + getRandomColor(4))));
getRandomColor('' + getRandomColor('d' + getRandomColor(7 + getRandomColor(4 + getRandomColor(0)))));
getRandomColor('' + getRandomColor('d' + getRandomColor(7 + getRandomColor(4 + getRandomColor(0 + getRandomColor(0))))));
getRandomColor('' + getRandomColor('d' + getRandomColor(7 + getRandomColor(4 + getRandomColor(0 + getRandomColor(0 + getRandomColor(2)))))));

getRandomColor('' + getRandomColor('d' + getRandomColor(7 + getRandomColor(4 + getRandomColor(0 + getRandomColor(0 + 2))))));
getRandomColor('' + getRandomColor('d' + getRandomColor(7 + getRandomColor(4 + getRandomColor(0 + 0 + 2)))));
getRandomColor('' + getRandomColor('d' + getRandomColor(7 + getRandomColor(4 + 0 + 0 + 2))));
getRandomColor('' + getRandomColor('d' + getRandomColor(7 + 4 + 0 + 0 + 2)))
getRandomColor('' + getRandomColor('d' + 7 + 4 + 0 + 0 + 2));
getRandomColor('' + 'd' + 7 + 4 + 0 + 0 + 2);

'd74002'
getRandomColor('');

getRandomColor('' + getRandomColor('d'));
getRandomColor('d' + getRandomColor(7));
getRandomColor('d7' + getRandomColor(4));
getRandomColor('d74' + getRandomColor(0));
getRandomColor('d740' + getRandomColor(0));
getRandomColor('d7400' + getRandomColor(2));

// reached max 6 characters for colorHexString
getRandomColor('d74002' + getRandomColor(whatever_and_skipped))

'd74002'

I’m not sure which is the most possible expansion and deduction. Anyone else have any idea about the correct expansion?

1 Like

thnx mate… for your kind suggestion