forked from rocketacademy/basics-github-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
43 lines (32 loc) · 1.23 KB
/
script.js
File metadata and controls
43 lines (32 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Please declare functions and variables above where they are used.
// Define two global variables to record the displacement of Tom and Jerry
//For every submission, The forward distance of Tom = input
//The forward distance of Jerry = random (10);
//We add the distance of Tom and Jerry to their global displacement
//We calculate the distance between Tom and Jerry
//Is less or equal to zero, Tom catch Jerry
//Global variables
var TomPosition = 0;
var JerryPosition = 20;
var main = function (input) {
var TomForward = Number(input); //How far Tom runs in this round
var JerryForward = getRandomInteger (10);
TomPosition = TomPosition + TomForward;
JerryPosition = JerryPosition + JerryForward;
var Distance = JerryPosition - TomPosition;
console.log (JerryForward);
console.log (TomPosition);
console.log (JerryPosition);
var myOutputValue = 'Distance is ' + Distance;
if (Distance <= 0){
myOutputValue = myOutputValue + 'Caught ';
};
return myOutputValue;
};
var getRandomInteger = function (max) {
// produces a float between 1 and max + .999999 etc.
var randomFloat = (Math.random() * max) + 1;
// take off the decimal
var resultInteger = Math.floor(randomFloat);
return resultInteger;
};