Basic HTML and JavaScript shell game concept

After spending several days working on this project, I am struggling to understand why the winning or losing message is not being displayed. The project involves a simple shell game created using HTML, JavaScript, and CSS. I have tried reworking the JavaScript code multiple times but still can't seem to identify the issue. Any guidance in the right direction would be greatly appreciated.

var noOfShells;
var noOfShells = 3;
var looplimit = noOfShells + 1;

function ballShell(shells) {

  var ballLoc = (Math.floor(Math.random() * shells)) + 1;
  return ballLoc;
}
document.getElementById('elTwo').innerHTML = ballShell();

var ballIs = ballShell(noOfShells);

function newShell(newID, ballIs) {
  this.shellId = newID;
  this.shellName = "Shell" + newID;
  this.ballIn = ballIs;
  this.hasBall = function() {
    var theId = newID;
    var theBall = ballIs;
    var checkMsg = "My number is " + theId + ". The ball is in shell " + theBall;

    if (theId === theball) {
      var checkMsg = checkMsg + " You Win! ";
      return checkMsg;
    } else {
      var checkMsg = checkMsg + " You Lose! ";
    }
  };
}
for (var i = 1; i < 4; i++) {
  this["shell" + i] = new newShell(i, ballIs);
}
var shellOneLink = document.getElementById('shellOne');
shellOneLink.addEventListener('click', shell1.hasball(), false);

function reloadPage() {
  location.reload();
}
var activateReload = document.getElementById('reloadLink');
activateReload.onclick = reloadPage;
ul {
  width: 100%;
  text-align: center
}
ul li {
  display: inline-block;
  width: 200px;
  border: solid 1px #ccc;
}
<link rel="stylesheet" type="text/css" href="css/base.css">
<header>
  <h1>Shell Game</h1>
  <nav>
    <ul>
      <li>
        <a id="shellOne" href="#">
          <img src="images/shell.jpg" alt="shell">
        </a>
      </li>

      <li>
        <a id="shellTwo" href="#">
          <img src="images/shell.jpg" alt="shell">
        </a>
      </li>

      <li>
        <a id="shellThree" href="#">
          <img src="images/shell.jpg" alt="shell">
        </a>
      </li>
    </ul>
  </nav>
</header>
<main>
  <h3 id="text-message">&nbsp;</h3>
</main>
<footer>
  <a id="reloadLink" href="index.html">Reload Page</a>
</footer>

Answer №1

The mistake you made was solely including your stylesheet without adding the javascript source file. To rectify this error, remember to add it in the following way:

<script src="myscripts.js"></script>

Answer №2

Below is an illustration of a straightforward shell game:

var doc = document, bod = doc.body;
function E(id){
  return doc.getElementById(id);
}
function ShellGame(displayElement){
  this.play = function(shellNum){
    var shell = Math.floor(Math.random()*3), r = 'You Lost!';
    switch(shellNum){
      case 0:
        if(shell === 0){
          r = 'You Won!';
        }
        break;
      case 1:
        if(shell === 1){
          r = 'You Won!';
        }
        break;
      case 2:
        if(shell === 2){
          r = 'You Won!';
        }
        break;
    }
    displayElement.innerHTML = r;
  }
}
var sg = new ShellGame(E('text-message'));
E('shellOne').onclick = function(){
  sg.play(0);
}
E('shellTwo').onclick = function(){
  sg.play(1);
}
E('shellThree').onclick = function(){
  sg.play(2);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to alter preact-material-components to switch to mdc-theme--dark mode, thereby changing the CSS style of all descendant components?

I recently created a preact-cli app and added the following code in Header.js: document.body.classList.add('mdc-theme--dark'); However, when a user tries to switch from the light theme to the dark theme, only the background of the app changes. ...

Passing multiple functions to child components in ReactJS as a single prop

I am utilizing the technique of passing multiple functions as individual props from the parent component to its child components. Everything is working correctly without any errors or problems, but I'm interested in exploring if there is a more effici ...

Exporting Data and Utilizing a Steady Data Table

I have incorporated the Fixed Data Grid into my latest project. https://facebook.github.io/fixed-data-table/example-sort.html My goal is to generate csv and pdf reports from the data displayed on the grid. Could you please advise me on how to export gri ...

Why is the startsWith function not returning the expected result in my code? What could be causing this issue?

I have a code snippet that sorts a list based on user input, but it fails if the user's input contains spaces. How can I modify the code to handle inputs with spaces without removing the spaces between characters? For example, if the user input is &ap ...

Integrating foundation-sites with webpack, unable to apply styles

Delving into the world of webpack for the first time has been quite a daunting experience! I'm attempting to set up the foundation for sites, but I feel completely lost when it comes to configuring it properly. Here is my Webpack configuration: var ...

Switch between two checkbox options by simply clicking on one without the need for JavaScript

Is there a way to select two combined checkboxes with one click? I have a small calendar where I need to mark weeks using checkboxes. Since one week can fall into two months, I have created duplicates of the same checkbox for each week. Is there a safe wa ...

Algorithm for Filling Tiles in a Gaming Environment

Background: In my current project, I'm developing a unique tile-based game in Javascript. The game involves a character who can freely move around the map without taking diagonal paths - only left, right, up, or down movements are allowed. As the cha ...

Personalized FullCalendar header title

Is there a way to display a unique header title for each calendar in my collection of 16? I've been trying various modifications to the code snippet below with no success: firstDay: <?php echo $iFirstDay; ?>, header: { left: 'prev,next ...

Are there any solutions to refresh a page by clicking a button in next.js?

I am currently learning how to work with next.js and I'm facing an issue where I need to reload my page to make a new API request. As a beginner, I'm not sure how to achieve this. I've tried a few methods but none of them seem to work. Below ...

The Challenges of Parsing HTML Source Code for Web Scraping

I've been attempting to scrape data from this website: (specifically rare earth material prices) using Python and BeautifulSoup. My query pertains not to Python, but rather the HTML source code of the site. When I utilize Firefox's "Inspect Elem ...

Move a 'square' to a different page and display it in a grid format after clicking a button

I am currently developing a project that allows students or schools to add projects and search for collaborators. On a specific page, users can input project details with a preview square next to the fields for visualization. Once the user uploads the ...

Use the angular cli to incorporate the CSS styles found in the node_modules directory

After successfully installing a new library with npm, I now face the challenge of importing the CSS into my project. It seems unwise to directly link to the node_modules folder. Can anyone suggest an easy way to import this CSS into my Angular CLI project? ...

Updating style of element in EJS following POST request in Node.js using Express

I am working on a form page that is supposed to display a Success Alert (Boostrap) once the user fills out the form and clicks the Submit button. <form class="well form-horizontal" action="/contact" method="post" id="contact_form"> ... (input fiel ...

How can you target the current component and use createElement within VueJS?

My goal is to gain access to the current component and generate a div within it once the component is ready. The following is the code snippet for my component, which demonstrates my attempt to target the this element and create a new div within it using ...

Having trouble setting a value for a textbox in angularjs

Greetings! I am currently working on a web application using AngularJS. My task involves binding data from various API's to a textbox in my project. Below is the snippet of the HTML code where I attempt to achieve this: <input class="with-icon" ty ...

How can I retrieve the offset top of a td element in relation to its parent tr element?

Here is some sample dummy HTML code: <table> <body> <tr> <td id="cell" style="height: 1000px; width: 200px;"></td> </tr> </body> </table> I am looking to attach a click event ...

Looking for regex to extract dynamic category items in node.js

Working on node.js with regex, I have accomplished the following tasks: Category 1.2 Category 1.3 and 1.4 Category 1.3 to 1.4 CATEGORY 1.3 The current regex is ((cat|Cat|CAT)(?:s\.|s|S|egory|EGORY|\.)?)(&#xA0;|\s)?((\w+)?([. ...

Heroku App Breaks Down - Fails to Render Static HTML Content (Express Server)

Despite my best efforts, I keep encountering this persistent error on Heroku whenever I attempt to serve a static HTML page with some basic JS, images, and CSS. I diligently followed all the advice from SO, made adjustments to index.js, restructured files, ...

Enhancing Request JSON Body Validation in Next.js 14 API Handler

I'm currently in the process of developing an API for my iOS application using Next.js 14. I have managed to get it up and running successfully, however, I am facing some challenges when it comes to properly validating the body of a request. ESLint is ...

Hovering over the child element, instead of the parent

I'm working on implementing a highlight feature for my website. The structure of the HTML looks something like this: <div> <!-- this is the main container --> <div> content ... </div><!-- a child element --> < ...