Click here to Skip to main content
15,912,837 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 0:55am

You are supposed to implement the following algorithm:
  • Enumerate the letters of first parameter.
    • If current letter equals second parameter, return true.
  • Return false.
 
Share this answer
 
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:
JavaScript
if(str1.includes(char) ) {


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

you can simplify the code to:
JavaScript
function stringIcludes(str1, char) {
    return str1.includes(char);
}
}
 
Share this answer
 
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. :)

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