I'm encountering an issue where my code is not executing, yet there are no error

The main focus of this project is to develop a rock paper scissors game utilizing functions and loops, among other features.

Displayed below is an outline of how the project is structured:

  1. The initial function generates a random choice for the computer.
  2. The second function prompts the user or player to input their choice.
  3. The third function executes the game and determines whether the player or computer emerges victorious.
  4. Lastly, there is a function that implements a for loop to run the game multiple times in order to determine the final winner.

I have been diligently working on this project, but unfortunately, I am unable to understand why it is not functioning correctly.


  // Your JavaScript code will go here

Answer №1

playRound() function appears to be missing return statements:

function playRound(playerSelection, computerSelection) {
  let result = '';
  if (
    (playerSelection == "rock" && computerSelection == "paper") ||
    (playerSelection == "paper" && computerSelection == "scissors") ||
    (playerSelection == "scissors" && computerSelection == "rock")
  ) {
    result = "Player loses!! " + computerSelection + " beats " + playerSelection;
    // ADD RETURN HERE

  } else if (
    (playerSelection == "paper" && computerSelection == "rock") ||
    (playerSelection == "scissors" && computerSelection == "paper") ||
    (playerSelection == "rock" && computerSelection == "scissors")
  ) {
    result = "Player Wins!! " + playerSelection + " beats " + computerSelection;
    // ADD RETURN HERE
  } else if (playerSelection == computerSelection) {
    result = "It's a tie!";
    // ADD RETURN HERE
  } else {
    result = "Euphoria";
    // ADD RETURN HERE
  }
}

Explanation The playRound() function must have return statements for each condition in order to properly evaluate the game outcome.

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

Does the instantiation of Node.js objects result in the creation of a unique object for each user?

I am currently following a tutorial on creating a RESTful API using Node.js. However, a particular part of the tutorial has raised some concerns for me. The tutorial involves instantiating an object called TaskRepository() to handle RESTful requests in the ...

Can one retrieve an express session using the sessionID given?

I have a NodeJS Express application with express-session that works well, however, it needs to be compatible with a cookie-less PhoneGap app. My question is: Can I access the data in an express session using the sessionID? I was thinking of adding the se ...

Uploading images simultaneously while filling out a form

Currently, I have a form that requires users to fill it out and upload an image. However, there is a delay of up to 30 seconds when the user hits "Submit" due to the image size being uploaded. I'm interested in finding a way to initiate the image upl ...

Exploring Nested Objects in Javascript

Currently, I am faced with the task of accessing and handling the response from an API call represented in this format: { BTC: { available: '0.77206464', onOrder: '0.00177975' }, LTC: { available: '0.00000000', onOrder: &apo ...

Is there a way to retrieve four random documents from a MongoDB collection using Node.js?

What is the best way to retrieve 4 random documents from MongoDB using node/express.js? I know how to set a limit for the number of retrieved documents, but not specifically for random selection. Any suggestions on how to achieve this? ...

Modifying a Property of Each Dynamic Object Within a Variable

I need to manage JSON objects containing a name and the token value Here is an example object stored in tokenHandler.json { "A Random Name":{ "token": 0 } } My objective is to create a function that increments the token value by 1 for eac ...

In Javascript (with Jquery), the increment and decrement operators (++/--), may produce incorrect calculations

I'm currently working on a slideshow project that involves two buttons, one for moving to the right and the other for moving to the left. However, I'm experiencing an issue where sometimes I have to double-click the buttons in order for them to w ...

most efficient method to execute numerous API requests at the same time

Currently, I am developing a backend using expressJS. Imagine that I need to make 10,000 calls to an API consecutively and store the obtained data in a database. What would be the most effective approach for achieving this task? Is it possible that Promis ...

Steps for creating a PDF file from an HTML page using JavaScript coding

I'm developing an HTML5 hybrid iPad app and need to create a PDF file of a report generated on one of the pages. I would like to save this PDF on the iPad. Can you provide assistance with achieving this task? I am utilizing JavaScript and mobile jQuer ...

Using PHP to output setTimeout() function to JavaScript

Currently, I have a PHP-based AJAX application that is responsible for loading videos onto a webpage. The challenge at hand involves setting up the application to load another video after a certain number of seconds retrieved from the database. My attempt ...

Failure to resolve security hole in package (svg-sprite-loader)

Update 3: I'm uncertain about the main issue here - whether it's a problem with the package itself or something I might be doing incorrectly when attempting to resolve it. It would be helpful to know if anyone has successfully installed the depen ...

Efficient File Upload Feature Compatible with all Browsers, Including Internet Explorer versions 7, 8, and 9

Can someone assist me with a problem I'm facing? I need a code script that enables multiple file uploading on all browsers. While HTML5 supports Chrome and Mozilla Firefox, it does not support IE. I am looking for a script/jquery solution that works ...

Issues encountered when updating MySql Database from WordPress with Error 500, whereas the code functions properly when used outside of WordPress environment

Question: How can I update a MySQL database from within a JavaScript function on a WordPress WooCommerce page using Ajax? I have a working code snippet for updating the database, but when I integrate it into my WordPress page, I encounter a 500 error. To ...

What could be causing this JSON file to be displaying in an unusual manner?

I am currently working with a JSON document that has been validated using JSlint. The JSON data is structured like this: [{ "date": "2017-02-10", " action": "Do a thing", "state": "closed", "url": "https:someurl.com" }, .... Additionall ...

Use an array to store nested JSON fields

I'm currently seeking to enhance my proficiency in utilizing JavasScript, React, and Material-UI. I am faced with the challenge of sorting my table using a nested JSON structure and I am encountering difficulties with assigning the JSON fields to my a ...

Ways to extract value from all chosen dropdown list elements

<table id="tb_Answers"> <tbody> <tr> <td> <select class="ddl_NextQuestion" name="_ctl0"> <option value="0">End</option> <option val ...

Ways to transfer a state from the child component to the app component

I have 2 different components that contain sub-components within them. In one of these components, I'm trying to figure out how to transfer the click event from the sub-component to another component so that it can render an entirely new component for ...

JavaScript function not functioning properly with variable input

I'm currently working on implementing two functions that will allow me to navigate through an array of image sources and display them in my image view: nextImage() and previousImage(). Everything seems to work fine when I hardcode the num variable, bu ...

Exploring request parameters within an Express router

I'm currently facing an issue with accessing request parameters in my express router. In my server.js file, I have the following setup: app.use('/user/:id/profile', require('./routes/profile')); Within my ./routes/profile.js fil ...

Struggling with eliminating a string from a JavaScript array

I can't seem to figure out the next steps. I've managed to remove all the duplicate numbers from the code, but now I need to also eliminate the strings without converting them to numbers. I'm feeling a bit stuck on how to move forward... le ...