Code your own adventure refactored

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.

Your example was a pretty eye opener for what to expect from javascript in a few months to a year :joy: My version of the code is a lot more basic but I’m struggling to figure out how to make a loop in this particular case and I was wondering if you could help.

confirm(“Let’s Play”);
var age = prompt(“What’s your age?”);
if (age < 13) {
console.log(“Seems you are a bit younger than my usual victims. By clicking ‘OK’ you
agree to enter at your own peril.”);
}
else {
console.log(“Sweet! Play on.”);
}

console.log(“You are on a long sandy beach that seems to have no end (perhaps
only because you aren’t looking for one). There is no Time here. At least no movement
of it.”);

console.log(“In the near distance, someone appears. They bare an uncanny resemblance
to someone you used to…It is them!”);

var userAnswer = prompt(“Do you want to speak to them?”)
if (userAnswer === “yes” || userAnswer === “okay”) {
console.log(“You break into a jog but the figure in the distance of your long
lost friend begins to fade. You run faster, trying to close the space between
you.”);
} // this is you crawling down the rabbit hole.
else if (userAnswer === “no”) {
console.log(“You ignore the random appearance of your long lost friend
who happens to be wandering the same timeless shore as you. After they
disappear, you feel your disappointment begin to gnaw at you.”)
};
else {
prompt(“yes or no?”); //I feel like a loop needs to go here…
** //what goes here?!**
} //game ends

I’m just trying to figure out how to finish what I presume is a loop… :confused:

This is covered in the JavaScript track so I don’t want to get ahead of where you are, at present. Recommend pour your energy into completing the track. I’ll be happy to help once you’ve completed it.