We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Okay, I am scratching my head here, I have the following solution, but the tests are failing (even though it appears like the tests SHOULD be passing. Is this because I was being too creative by leveraging Regex?
importReact,{useState}from"react";constOMITTED_WORDS=["a","the","and","or","but"];functionWordOmitter(){const[inputText,setInputText]=useState("");const[omitWords,setOmitWords]=useState(true);consthandleInputChange=(e)=>{setInputText(e.target.value);};consttoggleOmitWords=()=>{setOmitWords(!omitWords);};constclearFields=()=>{setInputText("")};constgetProcessedText=()=>{if(omitWords){constregexPattern="\\b("+OMITTED_WORDS.join("|")+")\\b";constregex=newRegExp(regexPattern,"gi");returninputText.replace(regex,"");}returninputText};return(<divclassName="omitter-wrapper"><textareaplaceholder="Type here..."value={inputText}onChange={handleInputChange}data-testid="input-area"/><div><buttononClick={toggleOmitWords}data-testid="action-btn">{omitWords?"Show All Words":"Omit Words"}</button><buttononClick={clearFields}data-testid="clear-btn">Clear</button></div><div><h2>Output:</h2><pdata-testid="output-text">{getProcessedText()}</p></div></div>);}export{WordOmitter};
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Word Omitter
You are viewing a single comment's thread. Return to all comments →
Okay, I am scratching my head here, I have the following solution, but the tests are failing (even though it appears like the tests SHOULD be passing. Is this because I was being too creative by leveraging Regex?