I'm creating a short game in the JavaScript console, and for inspiration, I looked at google's easter egg version (if you search text adventure then press f12 you can see it). I wanted input, the only way I could figure out how to have input was by having functions:
console.log('Do you like chocolate ice cream?');
function yes() {
console.log('I don't like chocolate ice cream.');
}
function no() {
console.log('Yeah, I agree. Who even likes chocolate ice cream anyways?');
}
But in google's version, you can just type yes, and it returns a bunch of text, no parentheses. I tried with variables but it didn't really look nice and it turned out green. Does anybody know how to make it google's way?
I'm creating a short game in the JavaScript console, and for inspiration, I looked at google's easter egg version (if you search text adventure then press f12 you can see it). I wanted input, the only way I could figure out how to have input was by having functions:
console.log('Do you like chocolate ice cream?');
function yes() {
console.log('I don't like chocolate ice cream.');
}
function no() {
console.log('Yeah, I agree. Who even likes chocolate ice cream anyways?');
}
But in google's version, you can just type yes, and it returns a bunch of text, no parentheses. I tried with variables but it didn't really look nice and it turned out green. Does anybody know how to make it google's way?
Share Improve this question edited Feb 6, 2022 at 17:11 asked Feb 4, 2022 at 19:00 user18122282user181222822 Answers
Reset to default 11I looked in the javascript console of the text adventure easter egg game, and I thought: "if google can do it, it must be possible to do it". And it sure is possible.
While looking through the game, I realised that all possible inputs were variables, which probably would have made sense because if it was anything else, you would probably get an undefined
error.
So I did a little bit of digging, on stack overflow, and I found that it's possible to listen for when a variable is called, like this:
Object.defineProperty(window, 'foo', {
get: function () {
console.log("hello world")
}
});
This listens for whenever the input foo
is called on the window and it will run the get function.
You can implement your code logic inside the get
function whenever the input gets called.
If you also want to style to your console, you can append "%c" to the start of your console message and add your css styles as a string as an argument to the next parameter. For example:
console.log('%cHello World!', 'color:blue;font-size:20px;');
TL;DR
Object.defineProperty(window, 'yourproperty', {
get: function () {
//your code logic
}
});
I hope this helped. This is my first stack overflow answer. This was very fun to look into.
You can't really do that. The closest you can get is
var input = prompt("What is your name?");
console.log("Hello, " + input + "!");