Lesson 2. why *2?


#1

https://www.codecademy.com/en/courses/javascript-beginner-en-mrTNH-6VIZ9/0/2?curriculum_id=506324b3a7dffd00020bf661

Everythings working fine. Just wanted to know why on the second line I'm supposed to * 2?


Replace this line with your code. 

var slaying = true;

var youHit = Math.floor(Math.random() * 2);

var damageThisRound = Math.floor(Math.random()* 5 + 1);

var totalDamage =0;


#2

The random() method returns a random number from 0 (inclusive) up to but not including 1 (exclusive).

and

The floor() method rounds a number DOWNWARDS to the nearest integer, and returns the result.

so if you don't do this it'll only give you 0

Run it :slight_smile:

var test=Math.random()
console.log(test)
var youHit = Math.floor(test);
console.log(youHit)

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.