re: Daily Challenge #276 - Unwanted Dollar Signs VIEW POST


This is my solution in javascript:

const money_value = (str) => parseFloat(str.replace('$','').replace(' ',''));

A more readable version:

const money_value = (str) => {
    let result = str.replace('$',''); 
    result = result.replace(' ','');
    return parseFloat(result);

My solution in Typescript

const remove_dollar = (input: string) => Number(input.replace(/\$|\s/g, ""));

I like this but I will do it without /g as you will encounter this replacement at the most once. Also space after $ maybe present or may not be so /\$?\s?/ would do I guess. What do you think?

Code of Conduct Report abuse