You pretty much almost have a working solution if you just change this line to use endsWith() on the original string argument instead (and flip your logic around).
Can you show me how you apply it?
Goddamnit responsibility.
MDN always has examples for things like these: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
I just donāt understand what he meant. I need to change that line with endsWith() with the original string? The original string was turned into an array.
EDIT: Why am I using endsWith() when itās an array?
The original string was not turned into an arrayāmost methods in JavaScript are not āmutatorsā and donāt change their arguments, but instead return a value given an argument.
arr = str.split("!");
This line saves the return value from the split() method into the arr
variable (you should add the āvarā declaration on this, btw). It doesnāt change the str
variable at all, it just leaves it alone. As I mentioned above, endsWith() should be used on the original string argument (the str
variable).
Yes, but this doesnāt workā¦
function intenseString(str) {
arr = str.split("!").endsWith("!!");
if(arr[1] === ""){
return true;
} else {
if(arr[2] === "" || arr.slice(-2) === "")
return false;
else
return true;
}
}
endsWith() is supposed to be used on strings, not arrays.
What using this function will do is check if the sentence has 1 of the criteria of an intense, which is to make sure that the sentence has at least 3 !s.
The endsWith() method canāt be used on arrays so you canāt chain it to the split() method, which returns an array. As I keep saying, you should use it on the str
parameter variable, which is a string, in the first line of the āelseā block:
if (str.endsWith()) // still needs the correct argument to work...
@rstorms You want to use endsWith
on the string before you do anything else. There is no sense in going on with the rest of the function if the string does not end with ā!!!ā.
You could do something such as:
function intenseString(str) {
// return false right away if the string does not end with '!!!'
if (!str.endsWith('!!!')) return false;
// the rest of your code
}
Why would you have !str?
!
is the not operator, so that condition is saying āif the string does NOT end with !!!, return falseā. This is giving us an early exit so the rest of the function does not have to run in cases where it should not.
Why wouldnāt you just say if the string ends with !!!, return true? Just adds more steps, right?
I thought this would be the solutionā¦I was wrong.
function intenseString(str) {
arr = str.split("!");
if(str.endsWith("!!!")){
return true;
} else {
if(arr[2] === "" || arr.slice(-2) === "")
return false;
else
return true;
}
}
You need to delete this line, itās malformed code and isnāt doing anything.
At this point I recommend taking a break and coming back to it tomorrow.
Rhetoricalā¦how do you make a grown man cry?
Well fucking A I got it:
function intenseString(str) {
arr = str.split("!");
if(str.endsWith("!!!")){
return true;
} else {
if(str.endsWith(""))
return false;
else
return true;
}
}
EDIT: NVM hahahaha of course it said I failed. Wellā¦it worked in the console.
Iām about to start throwing hands.
That code might give you the results you want for a very limited set of tests, but itās not actually going to work for all cases.
Rather than trying to continue with this problem, I recommend gaining a greater understanding of your larger obstacle, which is programming concepts. Iād recommend going through these courses if you havenāt done them:
Iām seriously this bad at this stuff? Damnit I feel terrible.