Having a bit of trouble with the task given to me. Here are the instructions:
"Strings are intense if they end in three or more more ! marks. However, having ! marks anywhere but the end makes for a non-intense string
Intense Strings
Hello!!!
This is an intense string!!!
Non-intense strings
Hello
This is ! not an intense string!!!
Also not intense!!
Implement the intenseString function below, where it will return true when an intense string is passed in, and false otherwise."
This is what they start you with:
function intenseString(str) {
// your code here
}
And this is what I have currently, but not sure what to do with the indexOf() methodâŚ
function intenseString(str) {
if(str.indexOf(>= "!!!"))
return true;
else(str.indexOf(< "!!!"))
return false;
else if(str.indexOf("''!''"))
return false;
}
function intenseString();
No, I am reading it right now and just too dumb to come up with a solution. I donât understand how I could only have two conditions and have this come out.
This is the key part in coming up with the solution. One way to do it would be by âremovingâ the last 3 characters from the string, and then checking for any exclamation points in that substring.
It seems we can combine 1 and 2. So we only have 2 things to focus on.
And as @astv99 generously gave us a possible method of approach.
1)Try to remove !!! from the end of the string. If we canât then we know itâs not intense.
2) If we can remove it, if search for another ! and we find one , then we know itâs still not intense.
My golden rule is, if you end up changing the original string with your function/algorithm/whatever, itâs best to make a copy of the original string.
No you donât need to, and there arenât any hard and fast rules as to when thatâs necessary.
The last hint Iâll give you is that itâs possible to solve this problem easily by taking advantage of JavaScriptâs indexOf() and substring() âfunctionalâ string methods, in a simple compound if-else block, as I already mentioned. Anything beyond that is overthinking the problem.
Well, to add to my previous post, if you had to take care of edge cases that have more than 3 exclamation points at the end, that makes it only a little bit trickier, but still relatively easy, and you can take care of those by using JavaScriptâs split() string method.
Actually, thereâs a deceptively simple way to solve the problem by using only the split() method and you donât even need the substring() or indexOf() methodsâŚ