Code-tests
// JavaScript Example - Function to calculate total price
function calculateTotal(items) {
// Calculate the sum of all item prices
return items.reduce((sum, item) => {
return sum + item.price * item.quantity;
}, 0);
}
// Example usage
const shoppingCart = [
{ name: "Laptop", price: 999, quantity: 1 },
{ name: "Mouse", price: 25, quantity: 2 },
{ name: "Keyboard", price: 75, quantity: 1 }
];
const total = calculateTotal(shoppingCart);
console.log(`Total: $${total}`); // Output: Total: $1124
# Python Example - Function to calculate total price
def calculate_total(items):
"""
Calculate the total price of all items in a shopping cart
"""
total = 0
for item in items:
total += item['price'] * item['quantity']
return total
# Example usage
shopping_cart = [
{"name": "Laptop", "price": 999, "quantity": 1},
{"name": "Mouse", "price": 25, "quantity": 2},
{"name": "Keyboard", "price": 75, "quantity": 1}
]
total = calculate_total(shopping_cart)
print(f"Total: ${total}") # Output: Total: $1124
Shopping Cart
Shopping Cart
Laptop
Price: $999
Quantity: 1
Mouse
Price: $25
Quantity: 2
Keyboard
Price: $75
Quantity: 1
Total: $1124
/* CSS for Shopping Cart */
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1 {
color: #333;
text-align: center;
}
.cart-container {
border: 1px solid #ddd;
border-radius: 8px;
padding: 20px;
background-color: #f9f9f9;
}
.cart-item {
border-bottom: 1px solid #eee;
padding: 15px 0;
}
.cart-item h3 {
color: #0066cc;
margin: 0 0 10px 0;
}
.cart-item p {
margin: 5px 0;
color: #555;
}
.cart-total {
padding-top: 20px;
text-align: right;
}
.cart-total h2 {
color: #cc0000;
}
import java.util.ArrayList;
import java.util.List;
public class ShoppingCart {
public static void main(String[] args) {
// Create shopping cart items
List- cart = new ArrayList<>();
cart.add(new Item("Laptop", 999, 1));
cart.add(new Item("Mouse", 25, 2));
cart.add(new Item("Keyboard", 75, 1));
// Calculate and display total
double total = calculateTotal(cart);
System.out.println("Total: $" + total); // Output: Total: $1124.0
}
/**
* Calculate the total price of all items in the cart
*/
public static double calculateTotal(List
- items) {
double total = 0;
for (Item item : items) {
total += item.getPrice() * item.getQuantity();
}
return total;
}
// Item class
static class Item {
private String name;
private double price;
private int quantity;
public Item(String name, double price, int quantity) {
this.name = name;
this.price = price;
this.quantity = quantity;
}
public String getName() { return name; }
public double getPrice() { return price; }
public int getQuantity() { return quantity; }
}
}
"Laptop", "price" => 999, "quantity" => 1],
["name" => "Mouse", "price" => 25, "quantity" => 2],
["name" => "Keyboard", "price" => 75, "quantity" => 1]
];
$total = calculateTotal($shoppingCart);
echo "Total: $" . $total; // Output: Total: $1124
?>
# Ruby Example - Function to calculate total price
def calculate_total(items)
total = 0
items.each do |item|
total += item[:price] * item[:quantity]
end
total
end
# Example usage
shopping_cart = [
{ name: "Laptop", price: 999, quantity: 1 },
{ name: "Mouse", price: 25, quantity: 2 },
{ name: "Keyboard", price: 75, quantity: 1 }
]
total = calculate_total(shopping_cart)
puts "Total: $#{total}" # Output: Total: $1124
using System;
using System.Collections.Generic;
using System.Linq;
namespace ShoppingCartExample
{
class Program
{
static void Main(string[] args)
{
// Create shopping cart items
var shoppingCart = new List-
{
new Item { Name = "Laptop", Price = 999, Quantity = 1 },
new Item { Name = "Mouse", Price = 25, Quantity = 2 },
new Item { Name = "Keyboard", Price = 75, Quantity = 1 }
};
// Calculate and display total
decimal total = CalculateTotal(shoppingCart);
Console.WriteLine($"Total: ${total}"); // Output: Total: $1124
}
///
/// Calculate the total price of all items in the cart
///
static decimal CalculateTotal(List- items)
{
// Using LINQ
return items.Sum(item => item.Price * item.Quantity);
// Alternative using foreach loop
/*
decimal total = 0;
foreach (var item in items)
{
total += item.Price * item.Quantity;
}
return total;
*/
}
}
// Item class
class Item
{
public string Name { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
}
}