The assignment requires you to write a Node.js function searchString() that rece
ID: 3817115 • Letter: T
Question
The assignment requires you to write a Node.js function searchString() that receives three parameters: a positive integer in the range of 1 to 20, a target string of characters of that length, and a single character. It then checks whether or not the target string contains the given character. If it does, this function returns the index of the target string where the character is located. Otherwise, it returns -1.
Your submission must consist of three files: your partner’s source code, the test suite for normal tests and abnormal tests, and a test result analysis. You cannot keep the source code of the function in the same file for the testing code; you need to separate the mocha/chai test files from the source code file.
Explanation / Answer
//FILE1 problem.js
exports.searchString=function(num,string,chara){
if(num<1 && num>2){
return -1;
}
else{
var result=[];
for(var i=0;i<string.length;i++){
if(string[i].indexOf(chara)!==-1){
//if(!string[i].indexOf(chara)) same condition
result.push(i);
}
}
if(result.length===0){
return -1;
}
return result;
}
}
//FILE2 test.js
var problem=require('./problem.js');
var sol=problem.searchString(6,"saira","a");
console.log("First solution",sol);//this is normal test case
var sol=problem.searchString(5,"saira","m");
console.log("Second solution",sol);//this in abnormal test case
//FILE3 test2.js
1)require the file test.js for excute the function in problmes.js pass the parameters you (range or numbers u given,string of length u given ,characters should be present)
it excute the function in problem.js
num size is not in the limit retrun -1
otherwise check each index position and store in array
finally return the array how places it is present
if array is null or not contain any elements not characters found in string so it result -1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.