December 2023
Intermediate to advanced
242 pages
5h 28m
English
| Recipe 36 | Using a Function as the Replacement Pattern |
Suppose your task is to improve an online real-estate marketplace that lists property specifications in square feet. You need to write a code that finds square feet values and provides a square meter version in parentheses so that international buyers can easily understand them.
Start with creating a function that converts a square feet value to a square meter (m2):
| | function convertToM2(sqft) { |
| | |
| | // remove non-digit characters |
| | sqft = sqft.replace(/\D/g, ""); |
| | |
| | // convert sqft to m2 |
| | const m2 = sqft * 0.0929; |
| | |
| | // round to two decimal places and return it |
| | return m2.toFixed(2); |
| | } |
Now, you need a function that receives a string as input and scans it for digits followed ...
Read now
Unlock full access