Cards(Am I missing something?


#1

while (currentCard !== 'Spade') {
console.log(currentCard);

var randomNumber = Math.floor(Math.random() * 4);

currentCard = cards[randomNumber];
}

Guys what are Math.floor and Math.random I have never seen them in the excercises?
In my opinion, in this part of javascript should be more excercises until cards because this is a very analytical language and some explanations are not enough.


#2

@betapro20865,

Weird, did you go through the initial exercises before Learn JavaScript 7.Loops?

https://www.codecademy.com/en/courses/learn-javascript/lessons/introduction-to-javascript/exercises/random

It's explained in the first lesson named:
1. Introduction to Javascript :
sublesson : 6.Random

Copy from that lesson:

Sometimes it's necessary to generate a random number within a program. We can do that with this code:

Math.random();

This code will return a random number between 0 and 1. JavaScript will generate a random number for us with this code.

The problem with this is that the answer will most likely be a decimal. Luckily, JavaScript has our back with another built in function called Math.floor. Math.floor will take a decimal number, and round down to the nearest whole number.

Hope it clarifies. :slight_smile:


#3

most likely => Math.random() can generate a zero, but just not very often. The generated numbers will always be less than 1. When it is not 0 it will be a floating point number, or float for short.

When we multiply by a fixed value greater than 1, the product will be a value somewhere between zero and that number.

0.2 * 4 == 0.8

When we floor that number, it comes back as 0.

0.6 * 4 == 2.4

the floor of which will be 2.

0.9999 * 4 == 3.9996

Soooo close to 4 but still less, so the floor is 3.

This works great with zero-indexing... [0, 1, 2, 3]. Notice we still have four cards.


#4

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