You are viewing a single comment's thread. Return to all comments →
Here is the solution:
import React, { useState } from "react"; const OMITTED_WORDS = ["a", "the", "and", "or", "but"]; function WordOmitter() { const [inputText, setInputText] = useState(""); const [omitWords, setOmitWords] = useState(true); const handleInputChange = (e) => { setInputText(e.target.value); }; const toggleOmitWords = () => { setOmitWords(!omitWords); }; const clearFields = () => { inputText && setInputText(""); }; const getProcessedText = () => { const inputTextArr = inputText?.split(" "); const filteredInputText = inputTextArr?.filter(word => !OMITTED_WORDS.includes(word)) if (omitWords) { return filteredInputText.join(" "); } else { return inputText } }; return ( <div className="omitter-wrapper"> <textarea placeholder="Type here..." value={inputText} onChange={handleInputChange} data-testid="input-area" /> <div> <button onClick={toggleOmitWords} data-testid="action-btn"> {omitWords ? "Show All Words" : "Omit Words"} </button> <button onClick={clearFields} data-testid="clear-btn"> Clear </button> </div> <div> <h2>Output:</h2> <p data-testid="output-text">{getProcessedText()}</p> </div> </div> ); } export { WordOmitter };
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 →
Here is the solution: