Coding problem solution not working

Know Your Neighbor // Edabit
I’m trying to solve this coding problem (:point_up_2:). But I’m not able to. Is this a hard problem? Or am I just too bad? Pls, be honest.
I have been working on this for 3 hours! YES REALLY!!
Please help me out.
Here’s my solution:-

function plusSign(str) {
	if (str[0]!="+" || str[str.length-1]!="+") {
        return false;
    for (index in str) {
        // console.log(index);
        console.log(index, str[index+1])
        if (Number(str[index]) == NaN) {
            if (str[index-1] && str[index+1]) {
                if (str[index-1] == "+" && str[index+1] == "+") {
                    console.log("LOGGED STR", str);



// plusSign("+f+d+c+#+f+") ➞ true

// plusSign("+d+=3=+s+") ➞ true

// plusSign("f++d+g+8+") ➞ false

// plusSign("+s+7+fg+r+8+") ➞ false

What’s the problem with the code?
Thank you!

Without code, what do you see as the step-by-step of this? If you were to look at the given string, how would you, in your mind or on paper, see if the condition had been met? What is the process backing your code?

It is a hard one, took me a few minutes to come up with a rough plan, but it is do-able. And freecodecamp is a great resource for challenges like this.

My approach uses regex, but yours doesn’t have to.

1 Like

I would rate this as a easy ish to medium problem, took me a few minutes to go through and think of what to do.

You are on the right track. I can see that you want to
1.: verify the string is itself encapsulated by “+”, then
2.: for every letter you find in the string, check it has “+” around it

It’s a solid plan, however for step 1, I propose a hypothetical string of “+a+=” which does not violate the restriction of all letters must be enclosed in “+”, yet will force your program to return false

For step 2, do you know of a better way to detect alphabets? perhaps by regex, or ascii code?

You are on the right track, good job so far, dont get discouraged!

EDIT: Your core issue is from not really understanding what they meant in terms of “only letters must be enclosed by +”, here letters meaning alphabets. That means numbers, symbols, whatever else aren’t restricted by the “enclosed by +” rule. So your test condition must account for that wider range of potential input.

Your algorithm is solid, and you are so very close to getting it. That to me means you are good at this, and you can get there.

1 Like

I’ll check if there is a plus sign at the very beginning and the very end of the string.
I’m going to go through all the characters of the string, then check if it is an alphabet. And if it is I’ll check if it has a plus before and after it. If all the alphabets have plus surrounding them, I will return True. Otherwise, false.

I just saw it is rated "VERY HARD’ on edabit. :exploding_head: . It made me feel a lot better.
And also this :-

So I’ll just assume you are just really good. And it’s not me that’s stupid.

Yes, I’m working to fix that.

I want to use regex. But I don’t know how to use it. Like, I know the concept of a regular expression, but not how to use it.

Thank you :slight_smile:

Oh, I get it now.

Thanks, again.

My thought process, which might well be needlessly complex, involves two regex: one to match a single letter character, and one to match a single regex character surrounded by plus signs.

str.matches() passed a regex, returns an array of matches. If the length of letter matches and the length of plus-letter-plus matches are the same…

Personally, i find regex strange. Which to my mind means i need more practice with it. is a useful place to practice.

It says “str is not a function” when I use this.

Are you doing str().matches? Because str is not a function, it’s a String. But strings have a .matches() function.

Again, not saying mine is the right approach, simply made sense to me.

1 Like

Don’t you mean match and not matches?


Ah crud. Thank you for rechecking me! Absolutely yes, String.match


I love that idea! I went ahead and coded it and the cool thing about this is you can code this fairly cleanly in very few lines of code. I did have to use some somewhat advanced regex syntax like lookahead/lookbehind, so depends on how comfortable with regex one is, I suppose one may find for looping through str to be more intuitive.


That sounds good! FCC has some solid regex lessons right within the JS curriculum. And I think you don’t have to get very far to know the required regex for your algorithm.

Good luck!


Why look ahead or behind? /\+[a-z]\+/i would work i think - a letter surrounded by plus signs.

Don’t know for sure, working at the moment and coding on my phone is a special kind of challenge.

That’s actually what I tried at first haha, but consuming regex like /\+[a-z]\+/i would miss potential matches like d in +c+d+e+, since the + before d would be consumed by the match made on c.

EDIT: and oh man mobile programming lol, I recently went on vacation with no laptop, tried to keep up the streak on LeetCode and that did not go well.

1 Like

Yep nope - you’re right. Doing without the lookaheads is actually consuming the letters. Only workaround for that would be to go letter by letter. So yep, the lookaheads and lookbehinds, which are a painful addition to an already rough regex. Here’s how it looks:

Maybe is better to go letter by letter, and simply check if the pre/post of that letter are plus? :thinking:

(twenty minutes later…) Tried the pre-post check in that same repl, called it plusTwo. They both work, I think. The trick with that one is, as @gururajankappa pointed out at the beginning, you need to check if both the first or last are a letter, and return false immediately in that case.

Next, you loop from the second to the next-to-last, checking if the current character is a string and if it is, that either the preceding or following are not a plus. In that case, we use the early-return pattern to return false.

If we get to the end of the string without returning, then every case was valid, and we can return true.

that was a fun challenge!

@gururajankappa, the issues I see looking back at your code is the, which doesn’t apply to arrays or array-like objects (strings are “array-like”). You could use a simple for loop, or for...of. In my case, a for loop worked fine.

Next, I’m not sure why you’re checking if the given character is NaN - I guess that’s a way to see if it’s a letter (or a plus, or a pound-sign, or something else). I simply used a regex, /[a-z]/i to check. the i on that tell the regex that it’s case-insensitive. If you find a match, then you check if it has an index+1 and index-1 (which I took care of by starting my loop at the second letter), then you check if those are both plus.

But within that for loop, you also don’t console.log true or false, or return a true or false from your function… which would likely also be an issue.


Okay, thank you. I will try it…NOW.

you have also some issues, like index is a string here

1 Like

Okay I’ll use that.

Okay I’ll use it too.

I logged it just to see what was happening :sweat_smile:

1 Like

How would I know the previous and next element of the string?

That was the main issue!!!
Thank you so much.