How to create varying if..else statements (if possible)?


#1

hey there, So I was completeing an assingment in codecademey involving searching for a certain string of letters in a bigger string of letters.

if(text[i] === "S"&&text[i+1] === "o"&&text[i+2]==="n"){
for(j=i;j<(i+myName.length);j++)
{
hits.push(text[j]);
}
The string I was looking for in this example was "Son" i was simpling using for loops to scan the text and look for this string. And then I was using .push to add the letters into an array.

BUT. i want to make this code more universal. I want to be able to enter any string, and have my code find that in a jungle of random text.

But what i sooon found out was that to achieve this I would need to create a varying if..else statement. An if statement which could increase or decease its conditions depening on how many letters were in the string that I was searching for.

So is this even possible? And if so How could I achieve it?

And, if a varying if..else statement is not possible, how can I do this? Assuming that there is no limit to the search string, and no limit to the text?

This is javascript, but im sure the process for doing it should be the same in many other languages, just different syntax.


#2

lets say you have a prompt:

userChoice = prompt("what is your name?")

what you could do is then check if text i matches the first letter of userChoice:

if(text[i]  == userChoice[0])

then, once you found the right letter, you could determine if the word is a match with a for loop:

if(text[i]  == userChoice[0]){
  match = true;
  for (j = 1; j < userChoice.length; j++){
     if (text[i+j] != userChoice[j]){
       match = false;
    }
  }
}
if(match){
  # the second for loop of your program to push the name into hits
}

What you propose is not really possible, getting to grips with programming logic can difficult. Hope this helps


#3

first of all WOW. second of all as you can see I am a novice programmer, so could you explain a couple of things? Why not set j=0? If j=1 wouldn't you miss the first letter of userChoice? since index starts with 0? And also wouldn't you need to put the final if statement--(the one which pushes to hits) inside of the loop, so that it pushes everytime? or is this just goign to test each index position in text and see if the following letters match user choice and then it will test the next one? What if there are multiple? This answered many of my questions, but it added alot of confusion because I have a smaller understanding, So could you please explain furthur?


#4

because you already checked this one with the if condition? why do it again? (DRY)

i just give you an idea of what is possible, i decided that i first would check if the name is a complete match, otherwise you start pushing things into an array, and then after 3 letters it turn out, it was not the word you where looking for (this can happen), you would have to remove the items you just pushed to the array.

okay, maybe i should have explained a bit more. once the correct letter is found the if statement:

if(text[i]  == userChoice[0])

evaluates to true.

then i created a variable match which i set to true. then i loop over userChoice to see if it is a complete match, if it is, match remains true. if it is not, at certain point this condition:

if (text[i+j] != userChoice[j])

will evaluate to true at some point, then match is set to false.

so now i have successfully determined if userChoice is a match to the word found in the string. if the word is a match to userChoice, match is true so this if condition:

if(match)

should be true, and you can start pushing it into hits array.

if the word is not a match, match is false, you can continue looping over your string/text.

does that help?


#5

Again you are Amazing dude, mind is blown :heart_eyes: I am truely amazed now. I understand what you mean now, thank you!


#6

good to hear :slight_smile: now see if you can implant it in your program