所以我有一個情況,我有兩塊數據我試圖從相同的循環(以及我希望它出來的相同的循環不有重複的代碼)。解決兩個變量在相同的循環
我正在尋找finalFloor我的數據數組也將帶我;但我也在尋找address []中的哪個索引變量currentFloor成爲負值。
下面是我的代碼,目前我運行這兩個獨立的函數(floorCalculator & inTheBasement),運行相同的代碼(不想要的,不好的編碼實踐),除了最終目標是什麼被發現。我非常努力想弄清楚如何結合這一點。任何想法或指針?謝謝您的幫助!
/* ----------------- 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
};
是的,它是一串7000個圓括號,其中(在地板上)落在地板上。所以我把字符串傳入.split(「」)將字符串拆分爲7000個字符並將它們存儲在數組地址[]中。然後想出最簡單的方法是使用for循環遍歷數組,同時詢問「up」或者「down」,然後在我當前的樓層上+/- 1。你能解釋一下你發送的代碼嗎?我對JS和jQuery仍然很陌生。 – rockchalkwushock
很高興,但首先:我認爲我現在瞭解你的代碼 - 你能澄清一下以確定嗎?你想'地下室'返回0如果它是否定的,並且不返回任何其他的東西? –
@Cody,讓我知道這是否工作。完全重寫了答案。我相信我瞭解你的代碼,但你必須讓我知道。 –