Skip to content

Commit 881ef40

Browse files
authored
Create 2288-apply-discount-to-prices.js
1 parent bbc58d5 commit 881ef40

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

2288-apply-discount-to-prices.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {string} sentence
3+
* @param {number} discount
4+
* @return {string}
5+
*/
6+
var discountPrices = function(sentence, discount) {
7+
const arr = sentence.split(' '), n = arr.length
8+
for(let i = 0; i < n; i++) {
9+
const cur = arr[i]
10+
const rest = cur.slice(1)
11+
if(cur.startsWith('$') && rest.length && !Number.isNaN(+rest)) {
12+
arr[i] = '$' + ((+rest) * (100 - discount) / 100).toFixed(2)
13+
}
14+
}
15+
return arr.join(' ')
16+
};

0 commit comments

Comments
 (0)