I am writing a small application for the user to enter the name of a book and then its price, push the values of those to an array, output the book name and cost to the page and then display the total.
the issue I am having is with the total, for example:
If I write 2 as the value of each of the values, the "totalOutput" says 022222 instead of 10 as I would expect, I have tried a few different things and read a few articles on here but haven't found any to be much help or that useful.
these are the exact lines I am having issues with:
//go over each item in price and add up the total
        price.forEach(function addNumber(value) {
            total += value;
        });
//write the total
    totalOutput.innerHTML = "the total value of the books is " + total;
}
And incase you need it - here is my full javascript code:
//Book shop task
function trackBooks() {
    //target the output ul and store in a variable
    var output = document.getElementById("booksOutput");
    //Setup the two arrays to hold the book names and their prices.
    var books = [];
    var price = [];
    //declare a variable for working out the total
    var total = 0;
    //target the total output
    var totalOutput = document.getElementById("totalOutput");
    //set up a counter for the loop
    var x = 0;
    //setup the loop for entering the names of the books and their prices, for the sample, I have set the loop to run 5 times as stated in the pseudo code
    for (var i = 0; i < 5; i++) {
        //add one to the counter on each loop
        x = x + 1;
        //declare a variable to ask the user for the book name and the cost and push those values to their arrays we created above
        var bookName = prompt("enter book name number " + x);
        books.push(bookName);
        var bookPrice = prompt("how much does book number " + x + " cost?");
        price.push(bookPrice);
        //create a variable to create new li tags on output 
        var newLi = document.createElement("li");
        //add the required info to the new link
        newLi.innerHTML = "book " + x + ": " + "<strong>" + books[i] + "</strong>" + " costs " + "<strong>" + price[i] + "</strong>";
        //write out the name and price to the page
        output.appendChild(newLi);
    }
    //go over each item in price and add up the total
    price.forEach(function addNumber(value) {
        total += value;
    });
    //write the total
    totalOutput.innerHTML = "the total value of the books is " + total;
}
 
    