Simplifying my code. How? Some parts are repetitive

javascript

#1


https://www.codecademy.com/PrimusAlphinex/codebits/fx5ScK/edit

I do not have a problem but I want to simply what is being repeated.


//Check if if/else statements are used. CHECK.
//Check if function(s) are used. 
//Check if for loops are used. 
//Check if booleans are used. 
//Check if arrays are used. CHECK
//Check if Variables. CHECK.

// Name: SPACE CONQUISTADORS!
// Creator: Primus Alphinex

//Make sure to uncomment this.
//var gameStart = prompt("Would you like play?");

if (gameStart === "yes") {
    alert("Great! Let's begin.");
    
    alert("The game is called SPACE CONQUISTADORS!");
    
    var gameIntro = prompt("The way this is going to go is that it is the end of the world on your planet, Earth, which is in the Milky Way Galaxy you have to escape the planet on a space ship find a place to reside. Still want to continue?");
    
    if (gameIntro === "yes") {
        
        alert("Very well then.");
        
        var name = prompt("Before we continue, what is your name? This is what you will be called in the rest of the game.");
        
        alert("News Reporter: Havoc has been unleashed all over the planet. Major cities, Paris, New York City, Berlin, Mexico City, Sydney, Hong Kong, Moscow, London, and Athens are economically collapsing, looting and anarchy are happening everywhere.");
        
        var whatDoYouThink = prompt("How will you respond? Stay here and die trying to help or obtain a space shuttle and a large professional team and run off to space and find a planet to repopulate. Type 'stay' to stay or 'run' to run.");
        
        if (whatDoYouThink === 'run') {
             
            alert("Let us continue then.");
            
            var encounterPeople = ["Qi", "Kara", "Alexa", "Steve", "William", "Raphael", "Pierre", "Sarah", "Tasha", "Zahmir", "Louis", "Amy", "Emma", "Catherine", "Angelina", "Ivan", "Gemma"];
            
            var randEncPeople1 = encounterPeople[Math.floor(Math.random() * encounterPeople.length)];
            var randEncPeople2 = encounterPeople[Math.floor(Math.random() * encounterPeople.length)];
            var randEncPeople3 = encounterPeople[Math.floor(Math.random() * encounterPeople.length)];
            var randEncPeople4 = encounterPeople[Math.floor(Math.random() * encounterPeople.length)];
            var randEncPeople5 = encounterPeople[Math.floor(Math.random() * encounterPeople.length)];
            
            var peopleCareer = ["a mechanic", "a doctor", "a software engineer", "a biogeneticist", "a poet", "an artist", "NASA flight pilot", "a hardware engineer", "a physicist", "a librarian", "a politician", "an entrepreneur", "a film director", "a photographer", "a welder", "a wood handyman", "a pharmacist", "a soccer player", "a semi-truck driver"];
            
            var randPeopleCareer1 = peopleCareer[Math.floor(Math.random() * peopleCareer.length)];
            var randPeopleCareer2 = peopleCareer[Math.floor(Math.random() * peopleCareer.length)];
            var randPeopleCareer3 = peopleCareer[Math.floor(Math.random() * peopleCareer.length)];
            var randPeopleCareer4 = peopleCareer[Math.floor(Math.random() * peopleCareer.length)];
            var randPeopleCareer5 = peopleCareer[Math.floor(Math.random() * peopleCareer.length)];
            
            var peopleFrom = ["Geneva, Switzerland", "Washington D.C., USA", "Sicily, Italy", "Siberia", "Queensland, Australia", "New Guinea", "Beijing, China", "Kiev, Ukraine", "Tokyo, Japan", "Brasilia, Brazil", "Tyre, Lebanon", "Instabul, Turkey", "Munich, Germany", "Stockholm, Sweden", "Winnipeg, Canada", "Anchorage, Alaska", "Mumbai, India"];
            
            var randPeopleFrom1 = peopleFrom[Math.floor(Math.random() * peopleFrom.length)];
            var randPeopleFrom2 = peopleFrom[Math.floor(Math.random() * peopleFrom.length)];
            var randPeopleFrom3 = peopleFrom[Math.floor(Math.random() * peopleFrom.length)];
            var randPeopleFrom4 = peopleFrom[Math.floor(Math.random() * peopleFrom.length)];
            var randPeopleFrom5 = peopleFrom[Math.floor(Math.random() * peopleFrom.length)];
            
            console.log("You met " + randEncPeople1 + " who is " + randPeopleCareer1 + " from " + randPeopleFrom1 + ".");
            console.log("You met " + randEncPeople2 + " who is " + randPeopleCareer2 + " from " + randPeopleFrom2 + ".");
            console.log("You met " + randEncPeople3 + " who is " + randPeopleCareer3 + " from " + randPeopleFrom3 + ".");
            console.log("You met " + randEncPeople4 + " who is " + randPeopleCareer4 + " from " + randPeopleFrom4 + ".");
            console.log("You met " + randEncPeople5 + " who is " + randPeopleCareer5 + " from " + randPeopleFrom5 + ".");
            
        } else {
            
// Extend this part with more content!

            alert("Very well then, die on the planet.")
            
        };
        
    } else {
        alert("Too bad.");
    };
    
} else {
    alert("You can come back if you want.");
};


#2

You can have a function checking for if statements.. basically

function checkIfNum1(variableName,stringCheck,funcIf, funcElse){
  if(variableName === stringCheck){
    funcIf();
  }else{
   funcElse();
 }
}

So you can call it like this

checkIfNum1(gameStart, "yes", function(){
  alert("Great! Let's begin.");
}, function(){
  alert("You Choose not to play...")
})

This is basically passing a function as an argument inside a function.. Hope this was helpful!