1

Ok, so bit stumped on this one. I created a function, this function adds a number to the front of my array elements according to the arrays length. The hope here is that the end user get's to see the index number of their book when viewing their book list.

var myFormData = [];

  function addArrayElement() {
    for(i=0; i < myFormData.length; i++){       
    myFormData[i] = i+1 + ": " + myFormData[i] + "<br>";
    }

How would I check if the individual array elements already had the number and the breakpoint applied on printing it to HTML

.

Basically I just need to be able to add the index number and break-point to new elements of the array as the user submits them instead of applying it to everything already submitted.

I see the logic errors, I know each-time the for loop runs it's iterating over every element of the array and adding an index number and a break-point. I don't know how I would give each element an unique identifier without the for loop.

Is this possible to somehow achieve the same effect, by storing the specific index number of the array element and then somehow tacking it onto the output as it is printed to screen. Like so?

document.getElementById('booktitle').innerHTML = INDEXNUMBER + ":" +myFormData+join("<br>);

I think the number would always be static like this though. I'am not sure.

I don't know if this code is relevant, but these are the functions I use to print books to the HTML element & my handling of data submission to the array.

      function printBooks() {
        clearBook();
        alert("Sorting your books");
        //Function within the default sort method
        //The inital array.sort(); is case sensitive and ruined the sorting procedure
        //Had to refactor it in my code to make it sort correctly
        myFormData.sort(function(a,b){
        a = a.toLowerCase();
        b = b.toLowerCase();
        if(a == b) return 0;
        return a < b ? -1 : 1;
        });
        addArrayElement();
        document.getElementById('booktitle').innerHTML = myFormData;            
    }



     function submitData()
                         {     
        value1 = document.getElementById("myTextBox").value;
        var boxCheck = document.getElementById("myTextBox").value;
           if (boxCheck.length < 1)
              {
                alert("Search field is blank")   
              }
           else {
              myFormData.push(value1);
             alert("Data Contents: " + myFormData);
             alert("You have submitted:" + " " + value1 + " " + "to your library." )
             document.getElementById('booktitle').innerHTML = myFormData;          
           }
        }

enter image description here

5
  • please add some example of the wanted result. Commented Nov 24, 2017 at 10:40
  • Added a picture of what happens each time a new array element is submitted. I just need to be able to add new data without applying the index number to everything every-time i submit something. Commented Nov 24, 2017 at 10:42
  • @ItzSunoItzSuno, I don't think your picture is that relevant here Commented Nov 24, 2017 at 10:43
  • the picture is relevant, i suggest to add numbers only for output, not inside of the data. Commented Nov 24, 2017 at 10:44
  • I changed the picture to be more explanatory. How would I apply the number to the output as a dynamic number? for each element like so 1:Book 2:Book 3: book without the for loop Commented Nov 24, 2017 at 10:46

1 Answer 1

0

Maybe you can make a check on your element to see whether it already contains "<br>" or not.

  function addArrayElement() {
    for(i=0; i < myFormData.length; i++){
    if(myFormData[i].indexOf("<br>") < 0)
       myFormData[i] = i+1 + ": " + myFormData[i] + "<br>";
    }

Or another way is to create another array that will be displayed instead of myFormData;

   var arrayToDisplay = [];
   function addArrayElement() {
    for(i=0; i < myFormData.length; i++){
       arrayToDisplay[i] = i+1 + ": " + myFormData[i] + "<br>";
    }

As a result your arrayToDisplay will always contain only index + value of previous array

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.