Beginner in need of help for basic JavaScript game. | SoloLearn: Learn to code for FREE!


Beginner in need of help for basic JavaScript game.

Hi all, I'm very new to JavaScript and well, programming in general actually! I've made a small "Name the City" game. So basically when the code is run on the Google Chrome console the game asks the user to choose a country and then asks what the capital city is. It's very basic and I've only added 4 countries and cities. What I would like to happen though is that once the user has played through once, it asks if the player would like to play again and then if Yes is selected it all starts again from the beginning. There are several things I've been playing around with. Maybe I need some sort of function for the "let promptCountry" and "let promptCity". I'm thinking one of the issues is that these are global variables and once the user plays through once is saves their response to each variable. Oh, I'm also aware I should be using arrow functions as per ES6 but for now I was just hoping to get my head around this problem then I'll convert to arrow functions after. I also probably need to wrap the whole thing in a loop but can't quite figure it out. Anyway if anyone could help It would be greatly appreciated. Thanks all! const database = [ {country: "England", city: "London"}, {country: "France", city: "Paris"}, {country: "Germany", city: "Berlin"}, {country: "Spain", city: "Madrid"} ] function check_city(country, city) { for (var i=0; i < database.length; i++) { if (database[i].country === country && database[i].city === city) { return true; } } return false; } function gameResponse(con, cit) { if (check_city(con, cit)) { alert("Well Done"); } else { alert("That is incorrect"); }} function play_again(play_again_response) { if (play_again_response != "Yes") { return("Goodbye"); } else { return("Here I would like the program to start again") } } let promptCountry = prompt("Choose a country"); let promptCity = prompt("What is the city?"); gameResponse(promptCountry, promptCity); let promptPlayAgain = prompt("Do you want to play again? (Ye

11/15/2019 4:45:38 PM

Shaun Williams

11 Answers

New Answer


Well yes most games are wrapped in a loop, the most basic is while gameIsRunning do handleInput() update() render() end cleanUp() In this case, I would do running = true while (running) // prompts here gameResponse(...) if(allSolved()) then if(promptPlayAgain()) initEverything() else running = false end The gameResponse would have to mark another array if the user got something right and check_city should return a boolean and index as correctArray = [false,false,false,false] check_city(c,c) for(...) if(...) return {res: true,index: i} return {res: false, index: -1} gameResponse(con, cit) let result = check() if (result.res) alert() correctAry[result.index] = true else alert() Finally allSolved() checks if the array is all true allSolved() for (i=0;i<correctAry.length();i++) if (!correctAry[i])return false; return true; and initEverything would just set everything in the correctAry to false


To create a restart feature, you need to declare variables in global scope. Declare a function to initialize these variables. Call init() at game start. Call init() at game restart.


And if you like to make games, this is a catalog of Game Development tutorials published by Happy To Help :


Hello Shaun Williams As you can see that your code text is truncated from character limits. For the next time, please save your code and share the link instead of pasting raw code text like this. Especially when the code is big (more than 15 lines). It's also more convenient for anyone to review a saved code rather than having to copy-paste the raw code text. For future reference, please follow these guides, for sharing links, and for general tips to gain better, valuable responses 👍


Little advice on asking a question to stay in the character limit: give a little background on what your program does, ask what you need/want, and show your attempt. You have all 3 but you also have extras. Long questions get ignored as they are "tl;dr" reducing your chance of getting an answer. What could be removed is: Hi all ... actually! It's very basic ... cities. There are several ... Thanks all! Of course you can ask your question however you want I'm just giving advice ;)


oh, you are not asking about the count feature, so Happy To Help's answer is more relevant. By the way, you can see the Simple Multiple Choice Quiz series for reference : final code: first post:


This is a small demo for this question


Hi Ipang, ok got it for next time. No problem. Thank you Happy To Help and Gordon for your help with my question. I'll play around with that and see what I can do :) Great day to everyone!


So it's cutting out the last two lines of the code which are: let promptPlayAgain = prompt("Do you want to play again? (Yes or No)"); play_again(promptPlayAgain); Thanks


Hi Coder, thanks for the tip 👍🏻