document.getElementById("button").onclick =
function() {
var str = document.getElementById("string").value;
console.log("The string you entered: ", str)
var letter = document.getElementById("letter").value;
console.log("The letter you entered: ", letter)
//var letterCheck = str.includes(letter)
//var letterCeck = str.indexOf(letter) !== -1
if(!str.indexOf(letter)) {
alert("Letter does not exist in the string")
}
else {
var afterLetter = str.substring(str.indexOf(letter)+1);
console.log("Substring:",afterLetter)
}
}
Here I want to implement the if statement if the letter I input does not exist in the string, and if the letter is there in the string it should execute else statement and print the output. The commented statements are the ways I ways trying to execute the if statement condition.
To better explain what I'm trying to do, this is the problem I'm working on: Create an HTML page with the following functionality i. It should ask the user 2 inputs:
- Enter a Sentence (any English sentence)
- Enter a Letter (any letter from A to Z) ii. On clicking on “Submit” button it should output the rest of the sentence after the first occurrence of the letter in the sentence iii. If the letter does not exist in the sentence, output -> “The letter does not exist in the sentence”.
iv. Example 1
- Sentence: Hello World!
- Letter: r
- Result: ld!
v. Example 2
- Sentence: Hello World!
- Letter: o
- Result: World!