I don’t get into this category very much since most users are able to help each other. However today here I find myself checking the instructions on one of the exercises and discovered some code saved which had been long ago forgotten.
Code to follow.
Sample output:
> What is your age?
> 10
We are not responsible for anyone under the age of 13.
—————————————————————————————
You are at a Justin Bieber concert, and you hear this lyric,
'Lace my shoes off, start racing.'
Suddenly, Bieber stops and says, 'Who wants to race me?'
> Do you want to race Bieber on stage?
> yes
You and Bieber start racing. It's neck and neck! You win by a shoelace!
—————————————————————————————
> How would you rate this game out of 10?
> 9
Thank you! We should race at the next concert!
—————————————————————————————
Bye!
Refactored code:
// Code your own adventure
// en/courses/javascript-beginner-en-x9DnD/0/7
// refactored by mtf
function hRule(n){
var a = new Array(n);
return "\n"+a.join('\u2014');
}
function abort(){
try {
notDef();
}
catch (e){
throw("Aborted");
}
}
function assent(p){
return confirm(p)? null : abort();
}
function lineIn(p,d,v){
var s, re = new RegExp(v,'gi');
while (s!==null && !re.test(s)){
s = prompt(p,d);
}
return s ? s : abort();
}
function echo(x,h){
console.log(x, h ? hr : '');
}
function show(x){
echo(" > " + x);
return x;
}
hr = hRule(32);
pStr = "Would you like to play a game?";
assent(pStr);
pStr = "What is your age?";
show(pStr);
if (show(+lineIn(pStr, null, '\\d')) < 13){
echo("We are not responsible for anyone under the age of 13.",1);
} else {
echo("Welcome! Enjoy the Game.",1);
}
echo("You are at a Justin Bieber concert, and you hear this lyric,\n'Lace my shoes off, start racing.'\nSuddenly, Bieber stops and says, 'Who wants to race me?'");
pStr = "Do you want to race Bieber on stage?";
show(pStr);
if (show(lineIn(pStr, null, 'yes|no')).toLowerCase() === "no"){
echo("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'",1);
} else {
echo("You and Bieber start racing. It's neck and neck! You win by a shoelace!",1);
}
pStr = "How would you rate this game out of 10?";
show(pStr);
if (show(+lineIn(pStr, null, '\\d')) > 8){
echo("Thank you! We should race at the next concert!",1);
} else {
echo("I'll keep practicing coding and racing.",1);
}
echo("Bye!");
If you use this code, which is highly adaptable, please link back to this topic. Thank you.