Here is the scenario: receiving a string input that looks like
Input text: string = "today lunch 200 #hotelname"
Output subject: "today lunch" price: 200 tag: #hotelname
My initial solution looks like this:
text: string = "today lunch 200 #hotelname"
tag: string;
price: string;
subject: string;
this.text.forEach(x => {
if(this.reg.test(x)){
this.tag= x;
}else if(parseInt(x)){
this.price = x;
}
else{
this.subject= x;
}
});
console.log(this.subject, this.tag, this.price);
However, my solution does not pass all the test cases. Is there a more efficient solution using TypeScript or JavaScript?
Test cases:
- The string cannot contain two numbers.
eg. 1: today lunch 200 #hotel 200
eg. 2: 200 food 2 0 1 #pasta - Any text starting with '#' should be considered a tag and there should not be multiple tags in a string.
- If the string is like "food at xyz hotel 100 #chinies"
Output: subject: "food at xyz hotel" price: 100 tag: #chinies