+ 1

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

15th Nov 2019, 4:45 PM
Shaun Williams
Shaun Williams - avatar
9 Antworten
+ 5
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. https://code.sololearn.com/WrZat1vLX669/?ref=app
15th Nov 2019, 5:39 PM
Gordon
Gordon - avatar
+ 5
And if you like to make games, this is a catalog of Game Development tutorials published by Happy To Help : https://code.sololearn.com/WvIsS7qN8npX/?ref=app
15th Nov 2019, 6:00 PM
Gordon
Gordon - avatar
+ 4
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 👍 https://www.sololearn.com/post/74857/?ref=app https://www.sololearn.com/Discuss/333866/?ref=app
15th Nov 2019, 5:12 PM
Ipang
+ 4
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 : https://code.sololearn.com/WKKkpq0efxai/?ref=app final code: https://code.sololearn.com/W3xgT0cpnO5n/?ref=app first post: https://www.sololearn.com/post/129437/?ref=app
15th Nov 2019, 5:43 PM
Gordon
Gordon - avatar
+ 4
This is a small demo for this question https://code.sololearn.com/WUJ7l0fzeUgn/?ref=app
15th Nov 2019, 5:58 PM
Gordon
Gordon - avatar
+ 1
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!
16th Nov 2019, 10:44 AM
Shaun Williams
Shaun Williams - avatar
0
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
15th Nov 2019, 4:57 PM
Shaun Williams
Shaun Williams - avatar
0
Hi Coder, thanks for the tip 👍🏻
18th Nov 2019, 2:23 PM
Shaun Williams
Shaun Williams - avatar