0

I've created my function that will fetch for my API to change the user's pace. In my changePace function, i've established my parameter pace, and that parameter will vary depending on which pace is selected by the user. I have 4 paces, Steady, Strenuous, Grueling and Resting. Any of which could be my parameter for the changePace function.

But when I try to call my changePace function with the appropriate parameter, I'm getting 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();
        });
    });
}
1
  • Where is steady declared? Commented Dec 6, 2018 at 23:19

1 Answer 1

1

I believe you intended steady to be a string, but you've written it as a variable, which you never defined.

Try changePace('steady'); instead.

Or, alternatively, you could define the variables somewhere, maybe near the top of the file. This is the recommended approach if you're going to use the 'steady' string in more than one place.

var steady = 'steady';
var strenuous = 'strenuous';
var grueling = 'grueling';
var resting = 'resting';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.