I have to make a function that has 3 parameters like (shirt, shoe, pant) and their price respectively 100, 200, 300 and quantity 10, 5, 3 respectively. how could I write the code and as a result I have to show
return totalSales ;
Show us the code you have now so we know what you have tried.
function totalSales(shirt, pant, shoe){
console.log(shirt, pant, shoe)
var total = shirt + pant + shoe;
return total;
}
var shirtPrice = 100;
var shirtSalesQ = 5;
var shirtSales = shirtPrice * shirtSalesQ;
var pantPrice = 200;
var pantSalesQ = 3;
var pantSales = pantPrice * pantSalesQ;
var shoePrice = 300;
var shoeSalesQ = 6;
var shoeSales = shoePrice * shoeSalesQ;
var total = totalSales(shirtSales, pantSales, shoeSales);
console.log(total)
I don’t really get the setup. Is the function really just supposed to add together the 3 parameters? Do you really just have free-floating variables? Is the multiplication calculation really just done at the top level before passing the result to the function. Because that seems like an odd setup.
Is this some assignment? If so what are the exact requirements you were given?
- function name: totalSales
- three parameters
- shirt price - 100
pant price- 200
shoe price-300 - so if now shopkeeper sales various quantities of shirts, pants, and shoes then what is his total sales and return only the numeric value of sales.
Where are the quantities coming from then?
I still do not understand what the function is supposed to do and I don’t get the parameters/arguments it is given. The way you are describing it doesn’t really make sense to me. That would just make it a function that takes 3 numbers, adds them together and returns the result. That seems a little to simple.
Are you sure the 3 parameters aren’t supposed to be the product, price, and quantity. Also, you would normally have some data structure like an array of product objects, not just free floating variables.
this is the data structure
const sales = [
{name: 'shirt', price: 100, quantity: 20},
{name: 'pant', price: 200, quantity: 10},
{name: 'shoe', price: 300, quantity: 14}
];
Yes, well that would make more sense. But why then were you writing the code as you did before?
We need the exact requirements and all the setup code you are given. I still have no real idea how you are being asked to do this based on what you have posted.
This would be one guess.
-
The function takes 3 strings. They are the names of the products.
-
You use the product strings to get to the product objects (object lookup based on the key, i.e. the parameters). You may need to loop the array.
-
You multiply the price and quantity for each of the product objects.
-
You add together the resulting multiplication of each of the products and return it.
yes you are right. could you give me the solution please.
No, sorry. That is not how we do things around here. The fCC forum is meant as a place to learn, we help people, we do not provide solutions. If you post your code we can help you.
Also, here is the standard reply we give. It might be worth following the advice.
Firstly, welcome to the forums.
While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.
With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).
It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.
Please provide some example of what you’ve tried and I’m sure you’ll get more help.
Happy coding
const sales = [
{name: 'shirt', price: 100, quantity: 20},
{name: 'pant', price: 200, quantity: 10},
{name: 'shoe', price: 300, quantity: 14}
];
let totalSales = 0;
for(const product of sales){
console.log(product);
const productTotal = product.price * product.quantity;
totalSales = totalSales + productTotal;
// return(totalSales)
}
console.log(totalSales)
i did this way but when I go for output then its showing all array and the result. but I just need result
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.