Please help check my understanding of this code


I was able to get the code to run properly, no error messages.

I wanted to make sure I understood how the code was running. I attempted to write out (in plain English) what happens as the code runs. I do understand how Math.floor/Math.random generates a value for youHit and damageThisRound so I didn't explain those elements.

I'm mainly seeking feedback about whether I understand what the code is doing while the game runs. Let me know what you think!

Here what I've come up with:

  • var slaying assigned a value of true, (a boolean value)
  • var youHit assigned a value by randomly generating a 0 or 1 (boolean true or false) calculated by Math.floor(Math.random * 2)
  • var damageThisRound assigned a value by randomly generating a number of 1-5 via Math.floor(Math.random * 5 + 1)
  • var totalDamage assigned a value of 0


  1. Evaluate slaying with a while statement
    If true, continue executing code (continue to step 2),
    If false, game over, while loop only continues as long as the condition is true

  2. Evaluate youHit with an if statement
    If true, evaluate code (continue to step 3)
    If false, evaluate code (continue to step 8)

  3. Execute console.log value, string plus damageThisRound (continue to step 4)

  4. totalDamage assigned a value of totalDamage plus damageThisRound (continue to step 5)

  5. Evaluate totalDamage with and if statement, is totalDamage greater than or equal to 4?
    If true, evaluate code (continue to step 6)
    If false, evaluate code (continue to step 7)

  6. Execute console.log, string value, you win!
    Assign slaying a new value of false
    Loop back to step 1, evaluate slaying with while statement, now false, game over

  7. Execute else statement
    youHit generates a random number either 1 or 0 (true or false)
    Loop back to step 2

  8. Execute else statement
    console.log, string value
    Assign slaying a new value of false
    Loop back to step 1, evaluate slaying with while statement, now false, game over


First off, let's review terninology.

float  =>  a Real number

A float is any number with a decimal point, even if the decimal fraction is 0. In truth, the floating point portion is the decimal fraction only. It will be less than 1 but could equal to 0.

Math.random() generates a floating point number.

var x = Math.random();

x will be a number between 0 (inclusive) and 1 (exclusive) so could be expressed by the following inequality:

{ x | 0 <= x < 1; x is Real}

Math.floor() is a downward rounding function. It always rounds down to the nearest integer.

Math.floor(Math.PI)  => 3

The expression,


will always be zero. If we add 1 then it will always be 1. In this, 1 is called the offset.

This expression,

Math.floor(Math.random() * 2)

will always be a value of 1 or 0. Since Math.random() is always less than 1 then multiplying by 2 will be less than 2, hence, 1 if Math.random() is greater than or equal to 0.5, and zero otherwise.


Thanks for your comment and insights. I did have a sense of how Math.floor and Math.random worked in this exercise but your explanation gives me more depth of understanding.

Any thoughts about the steps I wrote are welcomed as well :grin:


The numbers are not actually booleans, but can be evaluated to yield a boolean. The conversion is based upon truthiness. Consider,

0, '', "", null, undefined

are all falsy. The last term is not a keyword token, but the return value of a function with no return.value, or no return. It is also the value JS uses as a placeholder when a variable is not defined.

var a;
console.log(a);                 // undefined


console.log(console.log());    // undefined

a above is declared, so no error is raised. It has not been given a value, though, hence, undefined.

console.log() is a function with no return, so when we log the function, it returns, undefined.

So those are the falsy values, everything else is truthy, meaning it will yield a value of true in a conditional expression or render itself in a logical expression.

{x | x < 0; 
   | 0 < x; x is Real}
"a", "A"
function () {}

All are truthy

console.log(function(){} || 0);    // [Function]

console.log('a' || false);         // a

console.log(Math.PI || 0);         // 3.141592653589793

console.log({} || false);          // {}

console.log([] || false);          // []

The left hand operand short-circuits every expression above, and prints, meaning it is `truthy*. The right hand operand is ignored. Flip them around and the outcome will be the same, but no short-circuiting.

A lot of words to describe the non-boolean nature of objects and primitives, but how JS can convert based upon the evaluation.

It follows that the primitives, true and false are not truthy or falsy since they are booleans, and do not need conversion. That is why we never write them in comparisons.

if (a === True)    // incorrect usage
if (a)             // correct usage

Your program likely demonstrates this:

if (youHIt)

As we see from above, a can be any primitive or object, or expression. Note that function(){} is an expression, even while it is an object.

I'll let you absorb this and continue to read your summary.


I think you pretty well cover it. Two things that will help you a lot are, pseudo-code and flow charts. Both will help you to organize ideas and control flow in a more visual way, with less verbosity than prose, alone.

Pseudo code is essentially tokenized thinking. The ideas are expressed symbolically with tokens. Syntax never comes into play, only the ideas.. This code would then be ported to actual code.

Flow charts (I'm sure there must be a gazillion sites that offer some form of interface. or a program you can buy for a few dollars) use container symbols to represent the nature of a program component. Inputs, output, decisions, processes, etc. They help us graphically visualize logic and control flow, as well as general operations taking place in the runtime session.


Found this site, among others in a search for flow chart


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