This repository was archived by the owner on Oct 26, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy path4-tax.js
More file actions
90 lines (77 loc) · 2.4 KB
/
4-tax.js
File metadata and controls
90 lines (77 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
SALES TAX
=========
A business requires a program that calculates how much sales tax to charge
Sales tax is 20% of the price of the product
*/
function calculateSalesTax(sales) {
var tax = .2;
var salesTax = sales * tax;
var twoDecimals = salesTax.toFixed(2); // to add 2 decimal places
var twoDecimalsNum = parseFloat(twoDecimals); // converts text to number
return sales + twoDecimalsNum;
// var salesaddedTax = sales * 1.2;
// var twoDecimals = salesaddedTax.toFixed(2);
// return twoDecimals;
}
console.log(calculateSalesTax(17.5));
console.log(calculateSalesTax(34));
/*
CURRENCY FORMATTING
===================
The business has informed you that prices must have 2 decimal places
They must also start with the currency symbol
Write a function that transforms numbers into the format £0.00
Remember that the prices must include the sales tax (hint: you already wrote a function for this!)
*/
function formatCurrency(sales) {
function calculateSalesTax(sales) {
var tax = .2;
var salesTax = sales * tax;
var twoDecimals = salesTax.toFixed(2); // to add 2 decimal places
var twoDecimalsNum = parseFloat(twoDecimals); // converts text to number
return sales + twoDecimalsNum;
}
var number = calculateSalesTax(sales);
console.log(new Intl.NumberFormat('en-GB', {
style: 'currency',
currency: 'GBP'
})
.format(number)
);
}
// var number = calculateSalesTax(34);
// console.log(new Intl.NumberFormat('en-GB', {
// style: 'currency',
// currency: 'GBP'
// })
// .format(number)
// );
/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.
To run these tests type `node 4-tax.js` into your terminal
*/
function test(test_name, expr) {
let status;
if (expr) {
status = "PASSED";
} else {
status = "FAILED";
}
console.log(`${test_name}: ${status}`);
}
test("calculateSalesTax function - case 1 works", calculateSalesTax(15) === 18);
test(
"calculateSalesTax function - case 2 works",
calculateSalesTax(17.5) === 21
);
test(
"calculateSalesTax function - case 3 works",
calculateSalesTax(34) === 40.8
);
test("formatCurrency function - case 1 works", formatCurrency(15) === "£18.00");
test(
"formatCurrency function - case 2 works",
formatCurrency(17.5) === "£21.00"
);
test("formatCurrency function - case 3 works", formatCurrency(34) === "£40.80");