dimanche 21 août 2016

Solving for two variables in the same for loop

So I have a situation in which I have two pieces of data I am trying to get from the same for loop (well I want it to come out of the same for loop to not have repetitive code).

I am searching for the finalFloor my array of data will take me too; but I am also looking for at what index in address[] the variable currentFloor becomes a negative value.

Below is my code and currently I am running this as two separate functions (floorCalculator & inTheBasement) that run identical code (don't want, not good coding practice) except for the end goal of what is being found. I'm really struggling at trying to figure out how to combine this. Any ideas or pointers? Thanks for the help!

/* ----------------- Declaration of Variables ---------------- */
var up = '('; // represents moving up 1 floor.
var down = ')'; // represents moving down 1 floor.
var input_form = $('#input-form');  // represents the html input form.
var userInput = input_form.find('#address-input'); // represents the finding of the user's input.
var input; // stores user input value.
var address = []; // stores user's input value as an array of characters.
var currentFloor = 0; // represents the current floor in the for loop, set to ground floor (0).
var finalFloor; // represents the ending floor from the instructions given.
var results = $('.results');  // represents the div .results for appending data to.

/* ----------------- Parent Function ---------------- */
$(document).ready(initLoad);

/* ----------------- Child Functions ---------------- */
function initLoad()
{
  input_form.submit(function(event) // Listens for submission event at #input-form.
{
event.preventDefault();   // Prevents default method of html element.
takeInAddress();          // Calls function.
});
};


function takeInAddress()
{
  input = userInput.val();    // Stores the user input found at #address-input as var input.
  userInput.val('');          // Clears the input field for next user input.
  address = input.split('');  // Splits the string input into single characters stored now in the array address[ ].
  floorCalculator();          // Calls funciton.
};

function floorCalculator()
{
  for (var i = 0; i < address.length; i++)
  {
    if (address[i] == up)  // For any '(' present at the current index...
    {
      currentFloor++; // Increase the value of currentFloor by 1.
    }
    else if (address[i] == down) // For any ')' present at the current index...
    {
     currentFloor--; // Decrease the value of currentFloor by 1.
    }
  } // end for loop
  finalFloor = currentFloor; // Store the value of currentFloor now as finalFloor.
  // console.log(finalFloor);
  results.append('<h2>Floor to deliver to: ' + finalFloor + '</h2>'); // Append finalFloor value to .results html.
  inTheBasement();  // Calls function.
};

function inTheBasement()
{
  currentFloor = 0; // Resets currentFloor to zero.
  for (var i = 0; i < address.length; i++)
  {
    if (address[i] == up) // For any '(' present at the current index...
    {
      currentFloor++; // Increase the value of currentFloor by 1.
    }
    else if (address[i] == down)  // For any ')' present at the current index...
    {
      currentFloor--; // Decrease the value of currentFloor by 1.
      if (currentFloor < 0) // if currentFloor becomes a negative value...
      {
        // console.log(i);
        // Append value of i
         results.append('<h2>When you will arrive in the basement: ' + i + 'th instruction. </h2>');
         break; // break from loop
       } // end if loop
     } // end else if loop
   } // end for loop
  };

Aucun commentaire:

Enregistrer un commentaire