Check strings for "!"

Check strings for "!"
0

#1

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();


#2
  • This challenge probably expects you to use regular expressions.
  • I think that you might need to learn more about indexOf()
  • else statements don’t take a condition.
  • I’m not sure what you’re trying to achieve with this: "''!''"

#3
function intenseString(str) {
  if (str.indexOf("!") >= 3)
  return true;
  else if(str.indexOf("!") < 3)
  return false;
  else (str.indexOf("''!''"))
  return false;
}

#4

The indexOf() method doesn’t work the way you think it does, you can read up on it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf

And as ArielLeslie already mentioned, ‘else’ statements don’t take expressions. The basic form should look like this:

if (expression) {
    // 'if' body
} else if (expression) {
    // 'else-if' body
} else {
    // 'else' body
}

#5
function intenseString(str) {
  if (str.indexOf("!") >= 3)
  return true;
  else if (str.indexOf("!") < 3)
  return false;
  else if (str.split('').indexOf("''!''"))
  return false;
  else
  return true;
}

Have this now…


#6

I still think it would be better to use Regex. It would be easier in my opinion.


#7

You don’t need that many conditionals to solve this problem, it can be done simply with:

if (expression) {
    // 'if' body
} else {
    // 'else' body
}

Also it’s clear that you didn’t yet read the link that I posted, which you really should.


#8

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.


#9

Then let’s take a more high level approach.

What is your list of rules to determine if something is intense or not?


#10

A string with three or more “!” AND it only can be on the end. No interrupted strings.


#11

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.


#12

okay

  1. !!!
  2. !!! at the end
  3. No ! in the middle

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.


#13

Great, but would I need to create a new empty string variable? I’m always confused as to when I need to do that.


#14

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.


#15

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.


#16

I feel like I overthink really simple shit.


#17

You’ll get better, just practice.


#18

My syntax is wrong here?? The console is telling me my brackets are wrong…

function intenseString(str) {
  if(str.substring(0, -3).indexOf("!"){
      return false;
  }else{
      return true;
  }
}

#19

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…


#20

Ugh…why is this is so hard for me haha