Created
January 4, 2017 23:42
-
-
Save CharlyJazz/29f642bda2d16b1a63b7e0203240a851 to your computer and use it in GitHub Desktop.
Hackereacth - Benny And Shopping Solution in Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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