Requirements for generating random numbers in JavaScript. Can anyone help me understand how to implement this requirement effectively?

I've been experimenting with JavaScript to create a blackjack game, but I'm having trouble getting my code to work properly. My goal is for the getRandomCard() function to generate numbers between 1 and 13. Specifically, I want it to return 11 when the random number is 1, and 10 when the random number is 11, 12, or 13.

However, when I run my code, all it returns is the number 11. I can't figure out what's going wrong. Here's the snippet of code I wrote:

function getRandomCard() {
    let randomNumber = Math.floor(Math.random() *13) + 1
    if (randomNumber = 1) {
        return 11
    } else if (randomNumber = 11, 12, 13 ) {
        return 10
    } else {
        return randomNumber
    }
}

console.log(getRandomCard())

If you could provide some guidance on where I may have gone astray, I would greatly appreciate it. Thank you!

Answer №1

There are a couple of errors:

  1. if (randomNumber = 1) {

    The = sign is used for assignment, not comparison. To compare, you need to use either == or ===

  2. else if (randomNumber = 11, 12, 13 ) {

    This should be replaced with something like

    randomNumber == 11 || randomNumber == 12  || randomNumber == 12

    Alternatively, you can check if the number exists in an array using the includes function, e.g.

    [11, 12, 13].includes(randomNumber)

function getRandomCard() {
    let randomNumber = Math.floor(Math.random() *13) + 1
    if (randomNumber == 1) {
        return 11
    } else if ([11, 12, 13].includes(randomNumber)) {
        return 10
    } else {
        return randomNumber
    }
}
console.log(getRandomCard());

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

I'm working on updating a field within a Firestore document using ReactJS

I am encountering an issue while attempting to update a field within a document in Firestore using ReactJS. Interestingly, others have successfully done it without any errors. app.js const app = initializeApp(firebaseConfig); const auth = getAuth(app); co ...

What is the best way to customize the CSS of the Skype contact me button?

I am struggling with customizing the Skype contact me button. The standard Skype button has a margin of 24px and vertical-align set to -30px. I have tried removing these styles but have had no success. Here is the code snippet I am working with: Skype ...

The jQuery fadeToggle function toggles the visibility of an element starting from hidden instead

I'm having an issue where text in my div only appears on the second click, instead of the first. What could be causing this problem? $('#fPaperCirclePic').on('click', function () { $('#fPaperCircleText, #isargebla, #moq10 ...

Is it possible to add a personalized parameter to an unnamed JavaScript replace function?

I am looking to customize a date value in the following format: var d = new Date(); myobj.format(d, "dddd (ddd) S dd'd'.MM (MMM MMMM).yyyy HH:mm:ss.fff t tt T TT (o) {Z}"); I prefer not to utilize date.js because of its large size. The issue ...

Paper.js: Is there a way to prevent canvas height and width from changing when the window is resized?

My canvas with paperjs is set up to resize dynamically when the window is resized. I appreciate any help in advance. HTML <canvas id="myCanvas" height="800" width="1000"></canvas> JS var Initialize = function () { var canvas = document ...

Vue is struggling to load the component files

Lately, I've been encountering an error during webpack build that is causing some issues. ERROR There were 3 errors encountered while compiling The following relative modules could not be found: * ./components/component1.vue in ./resources/assets/ ...

Seeking assistance with setting up BxSlider installation

I'm struggling with adding bxslider to my HTML template. No matter what I try, the slider doesn't work as expected. When I preview my website, all three images are displayed together in a single column. Can someone please guide me on how to fix t ...

The functionality to disable and reenable a button after performing a calculation is not functioning properly in the code

Here's the issue I'm facing with my code: When I click the search button, a for loop prints "hi" 5000 times. Before this loop starts, I want to disable the button. However, after the console.log is done, the button should be enabled again. But fo ...

Server crashing as nodemon encounters mongoose issue

Currently, I am in the process of learning Node JS, Mongodb, and Express JS. My goal was to create a database using Mongodb Compass and store some data within it. However, every time I attempt to run my code, my nodemon server crashes after a few minutes o ...

Conceal dropdown options when there is no text entered

To achieve the functionality of hiding dropdown values when there is no text in the search bar, you can use the following code snippet. The code provided below works perfectly for single selection. However, if you allow multiple value selections, it automa ...

Error Alert: jQuery AJAX Event is not defined in Firefox Browser

This snippet works perfectly on Webkit browsers, but for some reason, it's not functioning in Firefox. I have a feeling that I need to include "event" within parentheses somewhere, but I'm unsure about the correct placement. Any ideas? html < ...

Looking for assistance with troubleshooting my isotope.js [code written in jquery and html]?

Explore this JSFiddle link I'm attempting to create sortable fancybox images, similar to the filtering functionality seen on this website. I previously achieved this with v1 of isotope but have been struggling with v2. After countless attempts and ad ...

Sketch a variety of numerical values in a circular formation

I was working on a number circle using the below fiddle, but I need it to always start from 0 at the top. How can I achieve this? Additionally, I would like to connect the numbers from the inner circle border to the number with a dotted line. How can I dr ...

"Experiencing difficulties deploying Firebase functions - Encountering an error message stating 'Cannot read property 'firebase-admin' of

I'm currently facing an issue during the deployment of my Firebase functions and I am seeking assistance to resolve it. While the deployment itself appears to be successful, I keep encountering the following error: Cannot read property 'firebas ...

Can CSS be used to create drop down menus?

Is it possible to create a dropdown menu using only CSS, similar to what you would find in most applications? I have a simple menu bar at the top of my screen and I want to be able to hover over an item like "Item1" and see sub-items appear below it. I en ...

Discovering the potential of utilizing an array transmitted by Node/Express on the server-side and integrating it into Google Maps on the client-side

When attempting to set up clustering markers on Google Maps, I encountered a challenge. Client-Side <script> // Code snippet from Google Map docs function initMap() { // Array of locations const locations = [ { lat: -31.56391, lng: 147.15 ...

Tips on how to retrieve the value of the second td in an HTML table when clicking on the first td utilizing jQuery

There is a specific requirement I have where I must retrieve the value of the second td in an HTML table when clicking on the first column. To accomplish this task, I am utilizing jQuery. $('.tbody').on('click','tr td:nth-child(1) ...

Script in Javascript halting Internet Explorer's operation

I'm encountering a problem with Internet Explorer freezing due to the following code. This code is part of a project that focuses on handling student grades: var array1 = StudentGradeAreadHugeList(); var nextArrayItem = function() { var grade = ...

Arrangement of Bootstrap card components

Each card contains dynamic content fetched from the backend. <div *ngFor="let cardData of dataArray"> <div class="card-header"> <div [innerHtml]="cardData.headerContent"></div> </div> <d ...

Achiever.js - Incorporating incremental progress with half stars instead of whole stars

Hello there! I've been utilizing Rater.js in my current project, and so far it has provided me with satisfactory results. However, I have encountered a particular issue that I am struggling to resolve on my own. It would be greatly appreciated if you ...