Hi All, i'm new to this forum && Javascript.
Currently employed as a QA Tester but have an interest in automation testing. As part of extra curricular learning I am starting on Javascript, then checking out Node.js and Selenium.js.
As part of a licence obtained through work I am going through some Pluralsight courses. Credits to the author, but I am wondering whether anyone can shed some light on this part of the code. I understand the logic behind the rest, just not this section.
<part I do not understand>
var survival = randomNumber(2);
Notes: Why is the parameter (2) and not () or (0)?
Is it because the array has 3 elements and giving a 33% chance?
<all the code>
// define the scenarios of the game
var beginningScenarios = ["You wake up in a hospital, it is quiet. You open the door and peek out...", "You are standing in a field with a house and a boarded up door...", "Desperate times call for desperate measures. You see a small store and decide to loot it for goods..."];
// create a function called randomNumber
function randomNumber(range)
{
return Math.round( Math.random() * range );
}
// using the randomNumber alert a scenario
alert( beginningScenarios [ randomNumber( beginningScenarios.length -1) ] );
// store weapons to an array
var weaponList = ["Shovel", "Crossbow", "Baseball Bat", "Rubber Chicken"];
// randomise the weapons
var weapon = weaponList[ randomNumber( weaponList.length - 1 ) ];
alert("Being that is it the apocalypse, you decide to search for a weapon first. After searching some nearby boxes you find a " + weapon + ".");
// attack the zombie
alert("Suddenly a zombie bursts through the door! You ready your " + weapon + " and advance towards the zombie!");
var survival = randomNumber(2);
// result of the attack
if (survival === 0){
alert("You swing your " + weapon + " & you miss.")
alert("The zombie bites you! Game over");
} else if (survival > 0) {
alert("You swing your " + weapon + " & you strike the zombie.");
alert("You kill the zombie. You win!");
}
Thanks for taking your time to answer my question!
Harry