Skip to content

Instantly share code, notes, and snippets.

@CharlyJazz
Created January 4, 2017 23:42
Show Gist options
  • Select an option

  • Save CharlyJazz/29f642bda2d16b1a63b7e0203240a851 to your computer and use it in GitHub Desktop.

Select an option

Save CharlyJazz/29f642bda2d16b1a63b7e0203240a851 to your computer and use it in GitHub Desktop.
Hackereacth - Benny And Shopping Solution in Javascript
var first = '4';
var two = 'I want to buy Microsoft for $10000';
var three = 'this house costs $0 5 0 00 0 Just buy it';
var four = '$ 1 2 3 4 5 6 7 8 9 hello123';
var five = 'the cost is zero $ 0 0 0 0 0 0';
function main(input) {
//Enter your code here
var dollar = '$';
output = input.replace(/\D/g,'');
if(output.length != 1){
if(output.match(/[^0]/g) === null){
output = '$0';
return output;
}
else{
dollar = '$';
dollar += output;
output = dollar;
r = /^\$[0]?\.?[0]/;
while(r.test(output) === true){
output = output.replace('$0','$');
output = output.concat('0');
}
return output;
}
}
return ''
}
console.log(main(first));
console.log(main(two));
console.log(main(three));
console.log(main(four));
console.log(main(five));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment