Logical error in JS

Hello there. I’m taking a Udemy full webdev bootcamp and its going well. However I can’t figure out this logical operator exercise and I’ve tried several things.

Here is the code:

const mystery = ''; //CHANGE THIS VALUE TO MAKE THE CONDITIONAL BELOW TRUE



// LEAVE THIS CODE ALONE! (pretty please)
if(mystery[0] === 'P' && mystery.length > 5 && mystery.indexOf('7') !== -1){
    console.log("YOU GOT IT!!!");
}

I can only change the const value so that it can print out the code below. I’ve tried several things but I’m officially stumped.
Please help.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Can you translate the conditionals in the if statement into English?
What are the requirements of mystery in order to print “YOU GOT IT!!!”?

Just a quick note, make sure you get the capitalization correct. It would have been more obvious if the letter H was used instead of P. But that might be the point as well.

To the already good advice, I would also suggest looking at each of those condition separately.

Before your if, I might put:

console.log(mystery[0] === 'P')
console.log(mystery.length > 5)
console.log(mystery.indexOf('7') !== -1)

The final logics are connected with && so you are trying to get each of those to evaluate to true.

1 Like

Good tip for next time thanks. I also didn’t know back ticks turn it in to colored code.

So… Were you able to define the three rules into your own words? A key part of being a dev is not only being able to understand a client spec, but also being able to decipher and explain someone else’s code.

3 Likes

Unfortunately I wasn’t. I’m still working on that issue along w/ others in the online bootcamp.

Well, what did you get when you tried to write those in words? What parts didn’t you understand? What specifically is confusing you?

1 Like

As snowmonkey is saying, understanding the problem is a key issue. It’s usually the first step. For example, if I had this problem:

if ((x > 12) && (x !== 15)) {

I would phrase that as:

if (x is greater than 12) AND (x is not 15)

So, those are my two conditions:

x is greater than 12
x is not 15

Since they are joined with && (AND), I need both of those to be true. Then I can think about what numbers can meet that requirement. For example, 13, 14, 16, etc. Basically 13 and up, skipping 15. (I’m assuming that we’re talking integers.)

You need to go through that process.

As a developer, you sometimes have to deal with complex logic. You need to be able to pick it apart. Not only that, but sometimes if you understand it well, you can simplify it.

2 Likes

So far nobody has mentioned to look into what the indexOf method is actually doing, as that is probably what is throwing you for a loop. Part 1 of the if statement is easy, it wants the first character in the string to be a capital P, the second part just wants the string to be longer than 5 characters, the last part is asking for the string to have ‘7’ in it otherwise the condition will not be satisfied.

solution

So for example Pabcd7 will work because its >5, starts with P, and contains 7.

2 Likes

But. [], .length and .indexOf() aren’t exclusive to strings. ["P","","","","",7] would also pass. Be careful of assumptions and expectations. :wink:

1 Like

Hoorray! Thank you Malin. It worked. I appreciate all the help and helping me understand the way to think through this.

Coding course help

We teach here. Please don’t just give people the answer. The OP would have learned a lot more if s/he looked up those prototype methods and learned how to do basic research. Instead you just gave a working answer.

5 Likes

@danpetroski you should have no problem solving this if you did learn something here.

const mystery = ''; //CHANGE THIS VALUE TO MAKE THE CONDITIONAL BELOW TRUE

// LEAVE THIS CODE ALONE! (pretty please)
if (
  mystery[3] === 'k' &&
  mystery.length > 3 &&
  mystery.indexOf('W') !== -1 &&
  mystery.indexOf('o') === 1
) {
  console.log('YOU GOT IT!!!');
}
1 Like

And do what w/ that exactly? Because if you’d like me to write that code in VS then…? What are you asking? And why? To prove that I learned and didn’t cheat? To explain what each step does?

You copied an answer that was given to you. If you can do the very similar challenge that lasjorg posted above, then you know that you understand what you copied and can use that knowledge to solve similar problems.

1 Like

I’m glad you said something because I did learn something but don’t get the ‘o’ index line. I thought the word ‘Work’ would be be the solution but I don’t think it is.

It satisfies the ‘k’ element and the length. But I’m not sure if it satisfies the other two.

Updated:
You know what, I’m sticking w/ ‘Work’ as the answer.

1 Like

Hey @danpetroski good day!

Let me break down the conditional for you so you can grasp the logic quicky!
I am sorry if this is late.

if(mystery[0] === 'P' && mystery.length > 5 && mystery.indexOf('7') !== -1){
    console.log("YOU GOT IT!!!");
}

It has 3 conditions:

  1. If The first index of mystery string is P
  2. And the length of the mystery string is greater than 5
  3. And if 7 is in the mystery string.

Points to note:

  • String is an array of chars. So it makes why we can index mystery[0] and use other array methods like indexOf and etc…
  • And array.indexOf(item) returns the index of the item within the array.
  • But if the item is not there in the array then, array.indexOf(item) will return -1

Happy Coding ^-^

Ah okay I think I understand. So then ‘Work’ should be the solution then.