Ask coding questions

← Back to all posts
supermarket
KareemMamdouh (1)

when i added the order function i finally could make more than one order and calculating the price but what i need to do is to add all the prices together from the same order to get the total

Commentshotnewtop
malvoliothegood (695)

Unless I am missing something your code looks unnecessary complicated to me. Why are you using so much recursion? If you need to call a function or section of code repeatedly use a for or while loop. Instead of the readline package you can use readline-sync like so:

const input = require("readline-sync");

function handleSearch() {
    let found = false;
    let searchTerm = input.question('What is the name of the item that you want to search for?: '); 
    // Trim whitespace and lowercase search term in order to do case insensitive comparison
    searchTerm = searchTerm.toLowerCase().trim();        
    for (let i = 0; i < items.length; i++) {
      if (items[i].name.toLowerCase() === searchTerm) {
	found = true;
	console.log("The unit price of " + items[i].name +" is " + items[i].price);
	break;
	}
    } 
    if (found === false) {
      console.log("Sorry, an item called " + searchTerm + " does not exist in the products inventory");
    }       
}
KareemMamdouh (1)

@malvoliothegood First of all thank you very much, but u didn't get my point my problem is in the handleOrder function i just wanna know how to add all the total of the order

malvoliothegood (695)

@KareemMamdouh Because of the seeming complexity of your code I found it difficult to follow your code. Being a programmer, I have this tendency to rewrite code so I can understand it better. All the best.