DEV Community

loading...

Discussion on: Daily Challenge #276 - Unwanted Dollar Signs

Collapse
mouadkh9 profile image
Mouad K.

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);
};
Collapse
lucasjg profile image
Lucas

My solution in Typescript

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

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?