mardi 23 mars 2021

Using Previous Result to Run Recursive Function in Javascript

I am trying to write a Javascript recursive function to help with calculations on another project, and I'm running into issues when trying to write out the function and get it to work as intended. I know some Javascript, but I'm no expert, so apologies if there may be some bad practices in my code. I have a function called quadraticSequencer that takes in an argument, num, and generates a quadratic sequence up to the given input num. It then returns the final output to be used and passed into the following function, subtraction. This is where the first issue occurs and the logic doesn't run as intended... ( where it should just return the last output in the sequence and pass it into the subtraction function, it seems to pass only the first output only and stops. )

The subtraction function takes in two arguments, num and nthTerm (which was returned and passed in from the quadraticSequencer function ), and runs a recursive function that redefines nthTerm as level and takes the level and subtracts it from num to generate a result, or const result = num - level. An if / else statement runs to check the result for divisibility using the modulo % operator, and if passes it console.logs result. If failed it moves to the next level (or iterative step down) and subtracts that from the PREVIOUS result and NOT num. That last part is critical because I'm not sure how to execute this logic correctly so that the previous result is stored for the next subtraction.

i.e. if num = 75, nthTerm would be 11, and the subtraction function would subtract 75 like so:

75 - 11. result = 64 || 64 - 10. result = 54 || 54 - 9. result = 45 || 45 - 8. result = 37 || 37 - 7. result = 30 || 30 - 6. result = 24 || 24 - 5. result = 19 || 19 - 4. result = 15 || 15 - 3. result = 12 || 12 - 2. result = 10 || 10 - 1. result = 9.

Hopefully, that makes sense. This is just how the subtraction has to run for our calculations to be accurate. If the first run-through finishes, it also does a "double-run" and adds more subtractions, basically subtracting in reverse and adding on more subtractions, not removing subtractions. So a full run with no passes would look like this: 75-11-10-9-8-7-6-5-4-3-2-1-1-2-3-4-5-6-7-8-9-10-11. Checking every result per subtraction. I wasn't sure how to write this within the if / else statement.

The overall expected output is to get the last output from the quadtraticSequencer function to pass it into the subtraction function for calculations, and the subtraction function be able to run as intended. So when num = 75, the subtraction function should pass when the level or "step" is 4 and the result = 15, or 75-11-10-9-8-7-6-5-4 = 15. 75 % 15 = 0. // 15

Here's my code:

const num = 75;

function quadraticSequencer(num) {
  for (let i = 1; i <= num; i++) {
    // generates quadratic sequence.
    let a = i;
    let nthTerm = a - 1;
    let b = 1 / 2;
    let quadraticSequence = b * a ** 2 + b * a;

    if (quadraticSequence <= num) {
      //   console.log(quadraticSequence, nthTerm);
    } else {
      false;
    }
    return quadraticSequence, nthTerm; // should return only the last results outputted from sequence.
  }

  function subtraction(num, nthTerm) {
    for (let level = nthTerm; level >= 1; i++) {
      const result = num - level; // subtracts the level from num to give a result.
      const previousResult = result - level;

      if (num % result === 0 || num % result === 1) {
        // checks result for divisibility.
        console.log(result);
      } else if (num % result !== 0) {
        // if condition fails, take result and subtract the next step down.
        result - level;
        previousResult - level;
      } else {
        for (let levelUp = 1; levelUp >= level; i++) {
          result - level; 
        }
      }
    } // this should run until it reaches the last "level" or step 1 and then reverses back up 
         to the original level.
  }
  subtraction(num, nthTerm);
}

quadraticSequencer(num);

Aucun commentaire:

Enregistrer un commentaire