Click here to Skip to main content
14,698,201 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a function called stringIncludes, which accepts two strings: the first string is a word and the second string is a single character.

The function should return true if the first string includes the character, otherwise it should return false.

Do not use the built in String.includes() function!

stringIncludes('awesome', 'e'); // true
stringIncludes('awesome', 'z'); // false

What I have tried:

function stringIcludes(str1, char) {
if(str1.includes(char) ) {
return true;
}
else {
return false;
}
}
('awesome', 'e')
('awesome', 'z')
Posted
Updated 7-May-20 1:55am

Quote:
What am I doing wrong in this code?

You didn't followed requirements!
you are requested to "Do not use the built in String.includes() function!" and your code uses it:
if(str1.includes(char) ) {


you forgot to tell the function name and the semi column at the end of lines:
('awesome', 'e')
('awesome', 'z')

you can simplify the code to:
function stringIcludes(str1, char) {
    return str1.includes(char);
}
}
   
Comments
CPallini 7-May-20 2:40am
   
5.
Patrice T 7-May-20 2:43am
   
Thank you
Richard MacCutchan 7-May-20 7:08am
   
But your code still uses String.includes().
Patrice T 7-May-20 7:45am
   
I didn't intend to do the homework in full :)
Richard MacCutchan 7-May-20 7:47am
   
Quite right too. :)
Vdiaz89 7-May-20 14:19pm
   
So, is there a way of writing the code without using string.includes()?
Patrice T 7-May-20 14:55pm
   
See solution 2
You are supposed to implement the following algorithm:
  • Enumerate the letters of first parameter.
    • If current letter equals second parameter, return true.
  • Return false.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900