Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7856153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:36:45+00:00 2026-06-02T20:36:45+00:00

I’m new to JavaScript. I’ve worked my work through Learning JavaScript (o’reilly) but am

  • 0

I’m new to JavaScript.
I’ve worked my work through Learning JavaScript (o’reilly) but am just trying to make my first JavaScript.

I thought it best to work on something I’m interested in and as it turned out is fairly complicated.

I’m basically trying to simulate (eventually) a situation in Space Hulk (Boardgame) where a Genestealer has 12 steps between him and the Space Marine.
On the first step its 6 on either dice to kill the Genestealer, and after that 5 or 6 to kill.
The gun jams if the number on the dice is the same.

I’m just trying to emulate the first step here. I think the problem is with jamCheck.

Basically this outputs as always true, even if I change it to != it always shows gun jammed.

I wondered if the variable needed to be passed into another local variable, but it works for killCheck without having to do this. (and I tried it, although I may be doing it wrong)

It is completely possible there is something really simple wrong here.

I hope you can help, or point me in the right direction.

Many thanks!

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
        <head>
        <title>SH</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript">
      //<![CDATA[

    function diceRoll1() {
       iValue = Math.random(); // random number between 0 and 1
       iValue *= 6; // multiply by 6 to move the decimal
       iValue = Math.floor(iValue)+1; // round to nearest integer. +1 to 1-6.
       var roll1 = iValue;
       document.getElementById('result1').innerHTML = 'Dice roll 1 : ' + roll1;
       killCheck (roll1);
       jamCheck (roll1);
       return;
    }

    function diceRoll2() {
       iValue = Math.random(); // random number between 0 and 1
       iValue *= 6; // multiply by 6 to move the decimal
       iValue = Math.floor(iValue)+1; // round to nearest integer. +1 to 1-6.
       var roll2 = iValue;
       document.getElementById('result2').innerHTML = 'Dice roll 2 : ' + roll2;
       killCheck (roll2);
       jamCheck (roll2);
       return;  
    }

    function killCheck(roll1,roll2){
        if (roll1==6 || roll2==6)
        {
        document.getElementById('kill').innerHTML = 'GS KILLED';
        }
        return;
    }

    function jamCheck(roll1,roll2){
        if (roll1 == roll2)
        {
        document.getElementById('jam').innerHTML = 'GUN JAMMED';
        }
        return;
    }   

    //]]>
    </script>
    </head>
    <body onload="diceRoll1();diceRoll2();killCheck();jamCheck();">
      <p id="result1">Dice roll 1</p>
      <p id="result2">Dice roll 2</p>
      <p id="kill">GS ALIVE</p>
      <p id="jam">GUN FINE</p>

    </body>
    </html>

EDIT: I eventually got there with a lot of help from a friend; here is the current code:

...
function getDiceValue() {
var diceValue = Math.random(); 
diceValue *= 6;
diceValue = Math.floor(diceValue) + 1; 
return diceValue;
}

function killCheck(roll1, roll2) {
if (roll1 === 6 || roll2 === 6) {
document.getElementById('kill').innerHTML = 'GS KILLED';
}
return;
}

function jamCheck(roll1, roll2){
if (roll1 === roll2) {
document.getElementById('jam').innerHTML = 'GUN JAMMED';
}
return;
}

function rollDice() {
var roll1 = getDiceValue(),
roll2 = getDiceValue();
document.getElementById('result1').innerHTML = 'Dice roll 1 : ' + roll1;
document.getElementById('result2').innerHTML = 'Dice roll 2 : ' + roll2;
killCheck (roll1, roll2);
jamCheck (roll1, roll2);
}
//]]>
...
<body onload="rollDice();">
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:36:48+00:00Added an answer on June 2, 2026 at 8:36 pm

    Math.floor() rounds down (think about the name… ceil rounds up) if you REALLY want to round to the “nearest” integer you need to use Math.round().

    In your case if you multiply by 6 and round down you’ll never get a number higher than 5.

    I suspect this is your problem, though I only glanced at your code, so, forgive me if that’s just one mistake and not the cause of your issue.

    [Edit] upon further reflection, disregard the above. The problem is that your methods expects 2 parameters but you’re only passing in one.

    I think you’re misunderstanding the way parameter passing works.

    jamCheck(p1, p2){} what you name these is not relevant. These labels only exist INSIDE your method. I suspect that what is confusing you is that you’re using the same labels for the variables you’re passing in, as well as the ones in your method. So, when you call the method jamCheck(roll1) it can’t do what it needs to cause it’s designed to work on 2 variables. Beyond that whatever result you’re getting is just the browser trying to make up for code whose syntax is broken. In languages like C or Java you wouldn’t even be able to compile such code; You’d be pointed at these lines as not making any sense, by the compiler.

    So, the solution is (something like)…

    var roll1,roll2;
    
    roll1 = diceRoll1();
    roll2 = diceRoll2();
    jamCheck(roll1,roll2);
    killCheck(roll1,roll2);
    

    But in your diceRoll methods the last thing you’ll need to do is return roll1; (or roll2 respectively)

    And look to kirean’s answer for how to wrap this all up in an init method, so that you’re not calling 4 (or more) methods from the body’s onload callback.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
I am trying to loop through a bunch of documents I have to put
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.