Javascript: Is there any way to clear history of user input?

445 views Asked by At

I'm creating a Simon Says game where the pattern gets progressively longer as the user gets the pattern correct, but the input history shows their previous pattern inputs. How do I clear this history?

This code is on the CodeHS site. I've researched and tried console.clear();, clear(); and console.API.clear();, but it just doesn't seem to work.

Here is the section of my code that gets the user's input.

//green red blue yellow
var generatedPattern = "grby"

function getUserInput(){
    /*User input of the last round gets saved which shows them 'grb', allowing 
    the user to look back at their previous guess to easily guess the 
    next pattern*/
    guess = readLine("Input guess here: (example: ybbg)");
    if(guess == generatedPattern){
        return true;
    }else{
        return false;
    }
}

I expect the log to be cleared with clear(); or console.clear();, but it doesn't do anything.

1

There are 1 answers

0
BJRINT On

You could do a custom function that outputs the ANSI escape character :

const clearTerminal = () => process.stdout.write('\033c')

(Please note that there should be a better way to handle your program, this helper function will just wipe clean your entire window, which is not what you might want)