From 509947109287cb095e3c4d7223869197af3b7070 Mon Sep 17 00:00:00 2001 From: Benito Rubano Date: Tue, 31 Dec 2024 09:09:29 +0100 Subject: [PATCH] solution/M1-020 --- projects/m1/020-discount-table/js/index.js | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/projects/m1/020-discount-table/js/index.js b/projects/m1/020-discount-table/js/index.js index e69de29bb2..b9cdf8423b 100644 --- a/projects/m1/020-discount-table/js/index.js +++ b/projects/m1/020-discount-table/js/index.js @@ -0,0 +1,43 @@ + +const DISCOUNTCode = 0.6; +const STARTINPrice = 4.95; +const SPACE = ' '; +const DefaultSpace = 10; + + +function getSpace(input,string){ + const inputStr=`${input}`; + if(string.length >= inputStr.length){ + return DefaultSpace + (string.length - inputStr.length) + } + + return DefaultSpace + (inputStr.length - string.length); +} + +console.log(`price${SPACE.repeat(DefaultSpace)}discount${SPACE.repeat(DefaultSpace)}newPrice`); +let result =''; + + + + +for(let i = STARTINPrice; i<= 24.95; i+= 5){ + const discountPrice = DISCOUNTCode * i; + + const newPrice = i - discountPrice; + result+=`${i.toFixed(2)}${SPACE.repeat(getSpace(i.toFixed(2),'discount'))}${discountPrice.toFixed(2)}${SPACE.repeat(getSpace(discountPrice.toFixed(2),'newPrice'))}${newPrice.toFixed(2)}\n`; + +} + +console.log(result); + + + + + + + +/*Write a program that uses a loop to generate a table for $4.95, $9.95, $14.95, $19.95 and $24.95 that shows: +- the original price +- the discount amount +- the new price for purchases +*/ \ No newline at end of file