I’m attempting to remove the last element from an array and return, or write that single element to a specific paragraph. However, I’m at a loss.
If someone could help please, it’d be much appreciated!
Ref: MDN web docs Array.prototype.pop() article, and W3Shcools JavaScript Array Methods article
Using the file “time-log.txt” for example, as such, (which is automatically generated by a lua script);
TIME: 312.05
TIME: 312.07
TIME: 312.10
TIME: 312.12
TIME: 312.14
html example;
<p id="writeData" class="data">Off-Line</p>
<p id="lastStamp">No Data yet</p>
and the Js example;
var txtFile = new XMLHttpRequest();
txtFile.open("GET", "time-log.txt", true);
txtFile.onreadystatechange = function() {
if (txtFile.readyState === 4) { // Makes sure the document is ready to parse.
if (txtFile.status === 200) { // Makes sure the file exists.
// no lines = txtFile.responseText; (The lines of text file are not seperated by a comma.)
// lines = txtFile.responseText.split("\n"); // Will separate each line of the text file by a by a comma.
var customTextElement2 = document.getElementById('writeData');
customTextElement2.innerHTML = txtFile.responseText.split("\n"); // (lines seperated by a comma)
/* This part works well enough as it writes to the #writeDate paragraph each line from the text file,
seperated by a comma, as to what I think is an array. */
} } };
txtFile.send(null);
/* Here, I'm attempting to write the last line only from above ("\n") to a seperate paragraph, #lastStamp,
however, I'm failing miserably */
var arrayedData = customTextElement2;
document.getElementById("lastStamp").innerHTML = arrayedData.pop();