Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e076482

Browse files
Add Calculate Tip to (L-B).
1 parent 7743307 commit e076482

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

‎L-B/0026 Calculate Tip/CalculateTip.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function calculateTip(totalCost, tipPercentage) {
2+
// Calculate the tip amount by multiplying totalCost by tipPercentage divided by 100
3+
const tipAmount = (totalCost * tipPercentage) / 100;
4+
return tipAmount;
5+
}
6+
7+
// Test cases
8+
console.log(calculateTip(50, 15)); // 7.5
9+
console.log(calculateTip(75, 20)); // 15

‎L-B/0026 Calculate Tip/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 0026 Calculate Tip ( L-B )
2+
3+
## Problem
4+
5+
You are at a restaurant, and you want to calculate the tip for your meal. Write a JavaScript function called **calculateTip** that takes two inputs: the total cost of the meal and the tip percentage.Your function should calculate the tip amount and return it.
6+
7+
**Example**
8+
9+
```
10+
If the total cost is 50ドル and the tip percentage is 15%, the function should return 7ドル.5 because 15% of 50ドル is 7ドル.5.
11+
12+
If the total cost is 75ドル and the tip percentage is 20%, the function should return 15ドル because 20% of 75ドル is 15ドル.
13+
```
14+
15+
- **_You can assume that the tip percentage is provided as a whole number (e.g., 15 for 15%) and that the total cost is a positive number._**
16+
17+
**Here's an example of how the function should work:**
18+
19+
```javascript
20+
console.log(calculateTip(50, 15)); // 7.5
21+
console.log(calculateTip(75, 20)); // 15
22+
```
23+
24+
## Solutions
25+
26+
```javascript
27+
function calculateTip(totalCost, tipPercentage) {
28+
// Calculate the tip amount by multiplying totalCost by tipPercentage divided by 100
29+
const tipAmount = (totalCost * tipPercentage) / 100;
30+
return tipAmount;
31+
}
32+
33+
// Test cases
34+
console.log(calculateTip(50, 15)); // 7.5
35+
console.log(calculateTip(75, 20)); // 15
36+
```
37+
38+
## How it works
39+
40+
1. We define a function calculateTip that takes two parameters: totalCost (the total cost of the meal) and tipPercentage (the percentage of the tip to be calculated).
41+
42+
2. Inside the function, we calculate the tip amount by multiplying totalCost by tipPercentage divided by 100. This is because tip percentages are usually provided as whole numbers (e.g., 15 for 15%).
43+
44+
## References
45+
46+
- [ChatGPT](https://chat.openai.com/)
47+
48+
## Problem Added By
49+
50+
- [Tipchan](https://github.com/tsongtheng)
51+
52+
## Contributing
53+
54+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
55+
56+
Please make sure to update tests as appropriate.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /