Rate variable input

After creating my custom function to fetch data from an API in order to change the user's pace, I defined the changePace function with a parameter named "pace". This parameter will be determined based on the user's selection of one of four paces: Steady, Strenuous, Grueling, or Resting.

However, when attempting to execute the changePace function with the correct parameter, I encountered the following error:

trail.js:8 Uncaught ReferenceError: steady is not defined
    at HTMLBodyElement.document.body.onkeyup 

JS FILE :

document.body.onkeyup = function(e){
    if(e.keyCode == 13){
        document.getElementById("paces").style.color = "white";
        paceDiv = true;
        console.log("Works");
    }
    if(e.keyCode == 49 && paceDiv == true) {
      changePace(steady);
      document.getElementById("paces").style.color = "black";
    }
    if(e.keyCode == 50 && paceDiv == true){
      changePace(strenuous);
      document.getElementById("paces").style.color = "black";
    }
    if(e.keyCode == 51 && paceDiv == true){
      changePace(grueling);
      document.getElementById("paces").style.color = "black";
    }
    if(e.keyCode == 52 && paceDiv == true){
      changePace(resting);
      document.getElementById("paces").style.color = "black";
    }
    if(e.keyCode == 32) {
      changeDay();
      document.getElementById("paces").style.color = "black";
    }
}

function changePace(pace) {
    fetch('/api/changePace',
        {method: "post",
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: '{"pace": "' + pace + '"}'
        })
    .then(function(response) {
        if (response.status !== 200) {
            console.log('Error: ' + response.status + "..." + response.value);
            return;
        }
        response.json().then(function(data) {
            changeDay();
        });
    });
}

Answer №1

It seems like in your code, steady is being mistaken for a variable instead of a string that you intended it to be.

You should try calling the function changePace('steady'); instead.

Another option would be to define the variables at the beginning of your file, especially if you plan on using the string 'steady' multiple times.

var steady = 'steady';
var strenuous = 'strenuous';
var grueling = 'grueling';
var resting = 'resting';

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

Sails JS - Flash message display issue on Heroku in production environment, works smoothly in development mode

I'm experiencing an issue with the flash message on my local machine during development, as it works fine there but not when I deploy the app on Heroku. I've been searching for a solution without any luck so far. api/policies/flash.js module.ex ...

Can anyone suggest a simple method to decode this JavaScript?

While browsing a website's checkout page, I came across a suspicious Javascript snippet that seemed out of place. It raised concerns that it might be secretly stealing credit card numbers: var R = ['1jBCeMi', '81AdhODE', 'keyd ...

Unlock the Power of Heroku: Leveraging Node.js to Share Environment Variables Across JavaScript Pages

Currently, I am following the 'getting started' guide on the Heroku webpage. As part of this process, I have cloned their tutorial repository and decided to add my own index.html and app.js files to the existing /public folder. The directory str ...

Node.js issue with redirect causing error message 'Cannot modify headers after they are sent'

While working with nodejs, I encountered an issue stating 'Error: Can't set headers after they are sent' when attempting to redirect after form submission. This problem was causing the application to crash. Below is the relevant code snippe ...

What could be causing the Quasar drawer to overlap the Quasar toolbar in a Vue application?

I am currently utilizing the quasar UI framework and have a main layout file that includes only a toolbar. This toolbar is meant to display across all pages in my application. <template> <q-layout view="hHh Lpr lff" style="backgro ...

Global axios configuration containing an undefined bearer token

After creating a Vue app using Axios that was installed via Vue UI dependency installation, I encountered an issue with the post headers returning an undefined token Bearer undefined. To resolve this, I made changes to my main.js file. Initially, I had add ...

Leveraging JavaScript event handlers within a progress wizard located within an update panel

I need assistance with implementing a password strength meter for a textbox within a wizard control. The issue I'm facing is that the password box does not become visible until step 4, preventing me from registering an event handler onload(). Placing ...

"Transforming portfolio photos from black & white to vibrant color with the click of a filter button

I'm currently working on creating a portfolio that includes button filters to toggle between black and white images and colored images based on the category selected. While I have successfully implemented the functionality to change the images to bla ...

Ensuring Filesize Verification Prior to Upload on Internet Explorer Using Javascript

Can JavaScript be used to verify a file's size before it is uploaded to the server at the client side? This application is developed using EXTJS and Java and is limited to Internet Explorer 7 on Windows XP machines. ActiveX cannot be used. The workf ...

A guide to implementing the map function on Objects in Stencil

Passing data to a stencil component in index.html <app-root data="{<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b5d4d7d6f5d2d8d4dcd99bd6dad8">[email protected]</a>, <a href="/cdn-cgi/l/email-pro ...

Iterating through an array of objects within a Vuejs template

Consider the JSON data below: { "games": [ { "id": 1, "init_date": "2020-02-11T07:47:33.627+0000", "players_in_game": [ { "id": 1, "player": { "id": 1, "player": "Jack Bauer", ...

Tips for handling form submissions within an Electron application on your desktop

I came across this question, but I'm still struggling to understand the best approach to handling forms in Electron. I am currently using the code snippet below and aiming to utilize dexie.js to store the form data upon submission. However, I'm ...

What is the best way to maintain a search input history while navigating search results and returning using the browser button, similar to Google Search in Angular2?

When I enter a keyword into a search input, press enter, and see the search results, I can then click on a result to navigate to its info. But how can I retrieve the originally searched keyword if I use the back button in my browser? ...

Implementing the ui-tinymce Directive Within a Different Directive

I am attempting to implement the ui-tinymce directive within another directive: angular.module("risevision.widget.common.font-setting", ["ui.tinymce"]) .directive("fontSetting", ["$templateCache", function ($templateCache) { return { restrict: ...

Unusual activity discovered when using JavaScript addClass and removeClass methods

While experimenting with animate.css, I created a custom Javascript function to initiate animation events. This simple function, triggered by an onClick event on a button, is only three (or four) lines long, but encounters a perplexing error. The anim fu ...

Having issues with v-link in vue.js

In my vue.js application, I have a route that looks like this: /edit/ride/:rideId When I try to link to this URL in my vue.js web app using the following code: <a v-link="{ name: 'edit/ride', params: { rideId: ride.id }}" class="btn-edit"&g ...

The SetInterval function is failing to display the current state

Even though the state has been set to true before calling the setInterval function, the useEffect hook is triggered with the new value of the state but it's not being reflected in the setInterval function. https://i.sstatic.net/xdrW4.png Check out th ...

Having trouble with jQuery div height expansion not functioning properly?

Having a bit of trouble with my jQuery. Trying to make a div element expand in height but can't seem to get it right. Here's the script I'm using: <script> $('.button').click(function(){ $('#footer_container').anim ...

Error: DataTables FixedColumn module "Uncaught ReferenceError: FixedColumns is not defined"

I am currently struggling to implement the FixedColumns plugin from datatables. I am following the example code provided on the website: $(document).ready( function () { var oTable = $('#example').dataTable( { "sScrollX": "100%", ...

Tips on implementing .on() with querySelector in jquery?

Can you help me convert the jQuery code below into a querySelector method? <script type="text/javascript"> jQuery(function($){ $('#woocommerce-product-data').on('woocommerce_variations_loaded', function() { $ ...