Jump to content

Can someone explain this parameter please?


astragtc

Recommended Posts

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

Edited by astragtc
Link to comment
Share on other sites

The function runs with a parameter representing the range of what will be a random rounded down number from 0 to 2, with 2 representing the max (range). This value is returned and will be the value passed to 'survival' variable. This variable value can then be processed depending on value, in the if and else conditions.

Link to comment
Share on other sites

20 hours ago, dsonesuk said:

The function runs with a parameter representing the range of what will be a random rounded down number from 0 to 2, with 2 representing the max (range). This value is returned and will be the value passed to 'survival' variable. This variable value can then be processed depending on value, in the if and else conditions.

Realised in my original post that I used the wrong terminology but that makes sense. Thanks for this.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...