Generated function should not call itself with its own arguments

Take this prompt:

/**
 * Returns a randomly-generated string of length len. The character char must be contained in the string exactly n times. The function checks that the character char is not contained in the string more or less than n times (charCountInString==0) before returning the string.
 * @param {number} len - The length of the string to generate.
 * @param {string} char - The character to include in the string. The character is wrapped inside a string, so that char.length == 1.
 * @param {number} n - The number of times to include the character.
 * @returns {string} A random string of length len. Contains char n times.
 */
function stringGen(len, char, n) {

I have been unable to generate a version of this that actually runs as intended, by the way. (You may notice how hard I tried to be specific that the character should not be in the string more or less than n times.) It made many that are pretty close, but will generate slightly longer strings than wanted, or just ignore the character that I asked for. But anyway, here is a common sort of result that I get with this prompt:

/**
 * Returns a randomly-generated string of length len. The character char must be contained in the string exactly n times. The function checks that the character char is not contained in the string more or less than n times (charCountInString==0) before returning the string.
 * @param {number} len - The length of the string to generate.
 * @param {string} char - The character to include in the string. The character is wrapped inside a string, so that char.length == 1.
 * @param {number} n - The number of times to include the character.
 * @returns {string} A random string of length len. Contains char n times.
 */
function stringGen(len, char, n) {
    var str = "";
    var charCountInString = 0;
    while (str.length < len) {
        var r = Math.floor(Math.random() * (len - str.length));
        if (r == 0) {
            str += char;
            charCountInString++;
        } else {
            str += String.fromCharCode(Math.floor(Math.random() * (127 - 32)) + 32);
        }
    }
    if (charCountInString == n) {
        return str;
    } else {
        return stringGen(len, char, n);
    }
}

You can see that at the end there, this function calls itself with its own arguments. Now, there’s recursion, and then there’s this. This isn’t useful at all, it just never halts. Sometimes I have erased the ending and added a comment after the else like // Handle the case where charCountInString != n. Do not call the function again. (The second sentence is necessary.) Then it writes something else. It doesn’t mitigate my problem since, try what I might, I haven’t got it to make a proper version of the function yet. But the fact that it generates those useless functions at the outset could probably have been caught by the AI trainers.