3

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Mizzmox
  • 31
  • 2
  • `.clear()` normally clears the console, not the history of input. Which usually isn't even managed by your program, but by the terminal itself. – Bergi May 22 '19 at 15:54
  • Why are you using the console instead of `` fields in the DOM? – Barmar May 22 '19 at 15:54
  • @Barmar Probably because it's a cli application (node.js), not running in the browser? (Would've been better of course to make this clear up-front) – Bergi May 22 '19 at 15:56
  • Yeah, someone needs to tell the people putting `autocomplete=off` in answers. – Barmar May 22 '19 at 15:58
  • Yes, i'm sorry, it's a cli application. Forgot to note that. – Mizzmox May 22 '19 at 15:59
  • 1
    Have a look at https://stackoverflow.com/q/24037545/1048572 for a similar issue – Bergi May 22 '19 at 15:59
  • [`readLine.createInterface`](https://nodejs.org/api/readline.html#readline_readline_createinterface_options) has a `historySize` option – Bergi May 22 '19 at 16:01
  • You should check Bergi's option, I believe it will lead you to what you need. – Mauricio Cárdenas May 22 '19 at 16:02

1 Answers1

0

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)

BJRINT
  • 639
  • 3
  • 8