In Javascript, the function sets a Boolean value to true but unexpectedly returns false

I have encountered an issue with my Audio Player. I created it in the document ready and set a variable for playing to false. However, even though the function seems to be called correctly, it does not set that variable to true as expected. The code for this function is triggered by a click listener.

var cartSongPlayer = new Audio();
var playingStatus = false;
var playingTrack;

function switchTrack(trackID, trackSource) {
    //logging basic information:
    console.log('Current Track ID ' + trackID)
    console.log('Current Track Source ' + trackSource)

    console.log('Previous Track ID ' + playingTrack)
    console.log('Playing Status ' + playingStatus)

    if (playingStatus) {
        console.log('song is playing')
        if (trackID == playingTrack) {
            playingStatus = false;
            console.log('pausing current song')
            cartSongPlayer.pause();

        } else {
            console.log('moved to new song')
            playingStatus = true;
            playingTrack = trackID
            cartSongPlayer.src = trackSource;
            cartSongPlayer.play();
        }
    //no song is playing
    } else {
        console.log('song is not playing')
        if (trackID == playingTrack) {
            playingStatus = true;
            console.log('resumed song')
            cartSongPlayer.play();

        } else {
            console.log('started new song')
            playingStatus = true;
            playingTrack = trackID
            cartSongPlayer.src = trackSource;
            cartSongPlayer.play();
        }
    }
}

Answer №1

It appears that there is a mix-up with the == comparison operator and the = assignment operator in your code.

Please update

playingStatus == true;

to

playingStatus = true;

Answer №2

The value of playingStatus remains constant.

Remember, the equality operator is ==, not the assignment operator (=).

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

Trying out the ClientPortal in Next.JS with Jest Testing

I'm currently working with NextJS and I want to run tests on the ClientPortal component. My testing toolkit consists of Jest and React Testing Library. Below is a sample code snippet for the ClientPortal component: import { useEffect, useRef, useStat ...

What is the new Bootstrap equivalent for btn-group-justify?

Could someone help me align the 3 radio options in this HTML code to make them appear justified? I have already tried using d-flex and btn-group-justify without success. If anyone could provide a sample using only Bootstrap 4.1+, it would be greatly appre ...

Accessing deeply nested objects from props

After pulling an object from my MongoDB database with Redux and passing it as props to a component, I am facing an issue where I can see the object in the console log but cannot access any information within the object. My objective is to extract specific ...

What is the best way to implement a switch case for the value of a property within an object in a TypeScript file?

The object I'm dealing with looks like this: {a: auth?.type === '1' || auth?.type === '2' || auth?.type === '3' ? { reason: // I need to implement a switch case here : un ...

Unresolved promise: Internal server issue

I encountered an exception while working on my Nativescript app. EXCEPTION: Uncaught (in promise): Server error JS: ORIGINAL STACKTRACE: JS: Error: Uncaught (in promise): Server error JS: at resolvePromise (/data/data/com.yourdomain.appname/files/app/ ...

Hiding rows in a Datatable

Assistance needed to conceal specific rows in the datatable, If the User opts for "Show All" from the dropdown menu, the entire datatable should be displayed, However, if the User chooses "Hide USA", I wish to hide the rows with the Country value set ...

Is there a way to specifically execute a Mongoose validate function solely for the create user page and not the edit user page?

Exploring Different Tools In the process of developing a website using Node.js, Express, and MongoDB. Leveraging mongoose for interacting with the MongoDB server has been quite beneficial. However, I encountered an issue where a function within my Mongo ...

The jQuery ajax request hangs indefinitely without triggering success or error callbacks

I have a jQuery ajax request to the server that triggers a redirect to a second page upon completion. It usually works fine, but in cases where the server response is delayed (e.g. 10 minutes), the callback function may not be executed, leaving the request ...

Tips for shifting a designated row upward in Chrome using JavaScript

Currently, I am working on a Javascript function that moves up a selected row. However, the issue I am facing is that when the row moves up, the old row is not being removed. For instance, if I move up the 'bbbb' row, it successfully moves up bu ...

Creating a text or JSON file in React using Node.js

I am completely new to React and have been practicing by creating a basic website. So far, I can render data from a JSON file and read data entered into a text box to display it in the log file. However, I am struggling with writing to a file. Can anyone ...

Ways to conceal various components while showcasing just a single element from every individual container

I am looking to only display specific span elements within their parent container (coin) while hiding the rest. The desired output should show only "1" and "first" while the other spans are hidden. <div class="types"> <div class=" ...

What is the process for reducing the value displayed on the label?

I have three labels and I would like to subtract the value of two labels and place the result in the third label. $(document).ready(function() { //Retrieve the values from both labels var value1 = document.getElementById("addition").innerText; ...

Unleashing the Power of Google Apps Script Filters

Having some data in a Google Sheet, I am looking to filter it based on specific criteria and return corresponding values from another column. Additionally, I need to count the number of elements in the resulting column. Below is an example of the data: Sa ...

Having trouble with my bootstrap slider carousel - it's just not cooperating

I incorporated Bootstrap's carousel to display the various courses on my website, featuring three courses at a time before transitioning to the next set of three. However, I am encountering an issue with this setup. Please see the image below for refe ...

I need to update my database by sending requests to my API, as the changes are not being reflected in the current database state. I am eager to see the

Struggling to grasp the concept of making requests to an API that uses express for CRUD operations. In the code snippet below, .get(/bears) displays a form and in app.post('/bears'), I'm using the 'request' module to send a request ...

Angular SPA boasting an impressive one million pages

Currently, I am in the process of transitioning my webshop, which contains numerous products, to an Angular SPA application using ngRoute and HTML5 mode. I have come up with a method where I receive some of my routes from the server as a JSON object, but s ...

``There seems to be an issue with the redirect header function in the PHP

Setting up my test site on a local host, I included an ajax request in one of my java-script files to a php script. if(hIF == "true"){ $.ajax({ type: "POST", url: "log_in/login.php", data: {name: userName, pwd: password}, ...

Cropper.js, effortlessly populating a container with a perfectly centered image

I took inspiration from the cropper.js library website but made modifications. You can check out the original example here. Here's my version of the modified example: CodePen Link var image1 var cropper1 var image2 var cropper2 ...

Dealing with errors in a sequelize ORM query: Tips and tricks

Currently, I am implementing Sequelize ORM in my Node/Express project using Typescript. Within the database, there is a 'users' table with a unique column for 'email'. As part of my development process, I am creating a signIn API and ...

Collapse all Bootstrap accordions with a single click of a button

Is there a simple way to toggle the collapse/expand functionality of all accordions with just one button click? I attempted to achieve this by using a basic button that would remove the 'active' class and add it back, but unfortunately, it didn&a ...