1

I am trying to update a specific number value based on another specific number value in javascript

I am looking for the best way to use javascript so that when the 'BPM' value changes, the 'time added' value would also change to match the following values:

BPM : Time added

-52 : 4.75

-48 : 4.5

-44 : 3.8

-40 : 3.2

-36 : 2.5

-32 : 2

-28 : 2

-24 : 1.5

-20 : 1.25

-16 : 0.85

-12 : 0.75

-8 : 0.5

-4 : 0.25

0 : 0

4 : 0

8 : -0.2

12 : -0.4

16 : -0.6

20 : -0.6

24 : -0.8

28 : -0.9

32 : -1.1

36 : -1.1

40 : -1.2

44 : -1.35

48 : -1.42

52 : -1.55

I believe this could be done with a series of if..else if statements, but that seems very unwieldy.

Here is the code I am using to update the BPM with a button click:

     function increase() {
        // Increase the playing speed by approx 1 BPM
        rate += 0.03808;
        bpm += 4;
    }

    function decrease() {
       // Decreasing the playing speed by 1 BPM
        if (rate > .25)
          rate -= 0.03808;
          bpm -= 4;
    }

I would like to add another variable called 'time change' that maps the value of the bpm to the correct time value in the list above each time the bpm is increased or decreased.

thanks

Matt C
  • 13
  • 2
  • I'm not really sure what you're asking here exactly, can you elaborate? Also 1 BPM is 1/60 Hz (roughly about 0.01667 Hz). Your `rate` doesn't seem to match your `bpm`. – M0nst3R Jul 08 '22 at 19:54

1 Answers1

1

You can do so with a javascript object. Just give each BPM a value in an object. Then attach an event handler to the BPM input and check to see if that value exists and use it. I'm just logging it for testing purposes.

let times = {
  "-52": "4.75",
  "-48": "4.5",
  "-44": "3.8",
  "-40": "3.2",
  "-36": "2.5",
  "-32": "2",
  "-28": "2",
  "-24": "1.5",
  "-20": "1.25",
  "-16": "0.85",
  "-12": "0.75",
  "-8": "0.5",
  "-4": "0.25",
  "0": "0",
  "4": "0",
  "8": "-0.2",
  "12": "-0.4",
  "16": "-0.6",
  "20": "-0.6",
  "24": "-0.8",
  "28": "-0.9",
  "32": "-1.1",
  "36": "-1.1",
  "40": "-1.2",
  "44": "-1.35",
  "48": "-1.42",
  "52": "-1.55"
};

let bpmInput = document.querySelector(".bpm");
bpmInput.addEventListener("input", function() {
  _time = times[bpmInput.value];
  if (_time) {
    console.log(_time)
  }
});
<input type="text" class="bpm">
imvain2
  • 15,480
  • 1
  • 16
  • 21