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.
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.
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.
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.
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.
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.
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.
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.