<head>
    <meta charset="UTF-8" />
    <title>Finding the longest word in a string using JavaScript</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="center-center">
      <span>( Finding the longest word in a string using JavaScript )</span>
      <h2 id="string">Stack Findover is the largest, most trusted online community for developers</h2>

      <button id="string_check" onclick="onbtnclick()">Click me</button>
    </div>
    
    
     <footer>
      <h2><a href="https://blogs.stackfindover.com/">Stackfindover Blog</a></a>
    </footer>
    
  </body>
* {
    padding: 0;
    margin: 0;
    font-family: 'IBM Plex Sans', sans-serif;
}
body {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    background: #f1f2f3;
}
h1#string {
    color: #0f62fe;
    font-size: 20px;
    font-weight: 600;
}
span {
    color: #FF5722;
    display: block;
    margin: 20px 0;
    font-size: 18px;
}
button {
    background-color: #0f62fe;
    border: 1px solid transparent;
    color: #fff;
    cursor: pointer;
    padding: 10px 20px;
    display: block;
    font-size: 18px;
    width: 100%;
    text-align: center;
    outline: 0;
    margin-top: 20px;
}
.center-center {
    max-width: 500px;
    margin: auto;
    position: relative;
}
.center-center:before {
    content: "{";
    position: absolute;
    top: 0;
    left: -100px;
    font-size: 180px;
    display: block;
    color: #0f62fe;
}
.center-center:after {
    content: "}";
    position: absolute;
    top: 0;
    right: -100px;
    font-size: 180px;
    display: block;
    color: #0f62fe;
}



footer a {
  text-decoration:none;
  color:#fff;
}

footer {
  position:fixed;
  left:0;
  right:0;
  bottom:0;
  text-align:center;
  background:#000;
}
 function onbtnclick() {
        document.getElementById("string_check").disabled = true;
        const str = document.getElementById("string").textContent;
        const findLongest = (str = "") => {
          const strArr = str.split(' ');
          const word = strArr.reduce((acc, val) => {
            let {length: len } = acc;
            if(val.length > len) {
              acc = val;
            };
            return acc;
          }, '');
            return word;
        };
        function matchString(){
          var string = str;
          var result = string.match(findLongest(str));
          document.getElementById("string").innerHTML = 'Output: '+' { '+ result +' } ' ;
        }matchString()
      }

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.