What's new
Roleplay UK

Join the UK's biggest roleplay community on FiveM and experience endless new roleplay opportunities!

Javascript help...

Conor Barcoe

Well-known member
Unlinked
Location
Manchester
Hi guys, so i cannot find a tutorial on this site or from any other sources and i dont really have the time to learn alot of javascript so i was wondering if anyone was able to help me. 

So i want to create a text game/simulator just for personal use, its hard to explain but it will be a screen that will have a counter (e.g. Humans: 100), so it is like a virtual world, overtime the number of humans will slowly increase but diseases can be created randomly which will select a random number of humans to kill and also every minute food will be found and lost, again a random number will be selected to decide what the variable will equal to, i think this code may be right, im a newbie and am trying to teach myself but this may work

var human = 200
var food = 100
if(food < human)
then some code which will reduce the amount of humans every minute etc.

But anyway i hope you get the idea and can help me, basically just a simulator of the world which is completely text base and i am able to add code that will change the world (basically playing god) such as 
var disease =1
if(disease >0 -1 human) I dont know but maybe you know the correct code to use  Thanks

This is what im trying to work on http://imgur.com/a/j4gcC

 
How do i make a variable randomly choose between set answers, so for example for the variable temperature, the answer will either be Hot or Cold, and it will change randomly every lets say 10 minutes?

Heres my current code

http://imgur.com/a/nwGgj

 
Last edited by a moderator:
I'd recommend you try googling this stuff. Not trying to be rude, but you won't get very far if you have to ask simple questions like that it will take a long time to learn anything. 

 
I'd recommend you try googling this stuff. Not trying to be rude, but you won't get very far if you have to ask simple questions like that it will take a long time to learn anything. 
I have tried trust me, but non of the code suits my current situation

 
Ok so, I don't know much JavaScript but languages are usually similar:

So I used the old google to look to see if there was a randomise function, as look would have it there is.

math.random() will be your answer in quite a few languages actually, there are funky parameters and things and stuff varies between languages but if you google it then it should be there.

That is assuming JavaScript uses something similar to Libraries or Modules, you may need to import these Libraries/Module/w.e. before using the function.

Then based of that random number you should be able to pick a variable.

So far I have only used Python and a mix of Base C and C++ so not much help in the JavaScript front.

 
I have tried trust me, but non of the code suits my current situation
No previous code will be perfect for a situation, that's why you learn what different functions do and then apply them in your own code. Although I can guarantee people have had very similar issues, as this is a very basic request.

As said above, Math.random() will do the job, making use of things such as setInterval on your loops for the timings.

Here's documentation on Math.random() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

- Marc

 
http://www.w3schools.com/

This site should help you out. You'll only learn by doing. If you want to do something, dont then say you dont have time. Make time.

 
http://www.w3schools.com/

This site should help you out. You'll only learn by doing. If you want to do something, dont then say you dont have time. Make time.


No previous code will be perfect for a situation, that's why you learn what different functions do and then apply them in your own code. Although I can guarantee people have had very similar issues, as this is a very basic request.

As said above, Math.random() will do the job, making use of things such as setInterval on your loops for the timings.

Here's documentation on Math.random() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

- Marc
Thanks guys, this is what i have currently created so atleast im getting somewhere xD

//VARIABLES
var Humans = 0;
var Food = 0;
var Disease = 0;
var Temperature = 0;
var Wood = 0;
var Metal = 0;
var Tools = 0;

//CONSOLE LOG
console.log("Welcome to Earth 0.1");

console.log("====================");

Humans = Math.floor(Math.random()*5);
        var humansOutput = "";
console.log("Humans:", Humans, "");        

Disease = Math.random(0,1);
        var disOutput = "";
        if(Disease <= 0.2){
        disOutput = "There is a Disease spreading!";
        }    
        else if(Disease > 0.2){
        disOutput = "No Diseases";
console.log("Disease:", disOutput)
Temperature = Math.random(0,1);
        var tempOutput = "";
        if(Temperature <= 0.1){
        tempOutput = "Extremely Hot";
        }
        else if(Temperature <= 0.4){
        tempOutput = "Hot";
        }
        else if(Temperature <= 0.7){
        tempOutput = "Cool";
        }
        else if(Temperature <= 0.9){
        tempOutput = "Cold";
        }
        else if(Temperature <= 1.0){
        tempOutput = "Extremely Cold";
        }
console.log("Temperature:", tempOutput);

Food = Math.floor(Math.random()*5);
        var foodOutput = "";
console.log("Food:", Food, "");

Wood = Math.floor(Math.random()*5);
        var woodOutput = "";
console.log("Wood:", Wood, "");

Metal = Math.floor(Math.random()*5);
        var metalOutput = "";
console.log("Metal:", Metal, "");
Tools = Math.floor(Math.random()*5);
        var toolsOutput = "";
console.log("Tools:", Tools, "");

//ALERTS
alert("Welcome to Earth version 0.1")
}

 
Thanks guys, this is what i have currently created so atleast im getting somewhere xD

//VARIABLES
var Humans = 0;
var Food = 0;
var Disease = 0;
var Temperature = 0;
var Wood = 0;
var Metal = 0;
var Tools = 0;

//CONSOLE LOG
console.log("Welcome to Earth 0.1");

console.log("====================");

Humans = Math.floor(Math.random()*5);
        var humansOutput = "";
console.log("Humans:", Humans, "");        

Disease = Math.random(0,1);
        var disOutput = "";
        if(Disease <= 0.2){
        disOutput = "There is a Disease spreading!";
        }    
        else if(Disease > 0.2){
        disOutput = "No Diseases";
console.log("Disease:", disOutput)
Temperature = Math.random(0,1);
        var tempOutput = "";
        if(Temperature <= 0.1){
        tempOutput = "Extremely Hot";
        }
        else if(Temperature <= 0.4){
        tempOutput = "Hot";
        }
        else if(Temperature <= 0.7){
        tempOutput = "Cool";
        }
        else if(Temperature <= 0.9){
        tempOutput = "Cold";
        }
        else if(Temperature <= 1.0){
        tempOutput = "Extremely Cold";
        }
console.log("Temperature:", tempOutput);

Food = Math.floor(Math.random()*5);
        var foodOutput = "";
console.log("Food:", Food, "");

Wood = Math.floor(Math.random()*5);
        var woodOutput = "";
console.log("Wood:", Wood, "");

Metal = Math.floor(Math.random()*5);
        var metalOutput = "";
console.log("Metal:", Metal, "");
Tools = Math.floor(Math.random()*5);
        var toolsOutput = "";
console.log("Tools:", Tools, "");

//ALERTS
alert("Welcome to Earth version 0.1")
}
Looking pretty good so far mate! I can also say that I have personally used W3Schools and was definitely the most helpful way for me to learn and improve on using javascript !

 
Looking pretty good so far mate! I can also say that I have personally used W3Schools and was definitely the most helpful way for me to learn and improve on using javascript !
Cheers, it my first time making something on my own and have only had a couple of lessons on coding in college as it mainly focuses on the other aspects of it such as security, im surprised my code works xD

 
Back
Top