css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <h3>Returns 1 if the word is valid in a game of Scrabble.</h3>
<input type="text" id="scrabble-word"></input>
<button id="scrabble-search">Search</button>
<div id="response-message"></div>
            
          
!
            
              body {
   font-size: 20px;
   margin: 50px;
}

input,
button {
   font-size: 20px;
   margin-top: 5px;
   margin-bottom: 15px;
}
            
          
!
            
              var API = {
   
   CORS: 'https://cors-anywhere.herokuapp.com/', //needed to get around HTTPS vs HTTP conflict
   baseURL: 'http://www.wordgamedictionary.com/api/v1/references/scrabble/',
   key: '7.304453775081076e29',
   
   generateURL: function(wordToCheck) {
      //takes in a word and returns a properly formatted URL for the API
      return this.CORS + this.baseURL + wordToCheck + '?key=' + this.key;
   },
   callAPI: function(word) {
      //generate a URL for the specified word, then call the fetch API
      return fetch(API.generateURL(word), { method: 'get' })
      //get the response in a string format
      .then(response => response.text())
      //parse the response to XML
      .then(str => (new window.DOMParser()).parseFromString(str, "text/xml"))
      //store the response in the wordInfo object
      .then(data => wordInfo.apiResponse = data)
      //check the response xml for whether it's a scrabble word
      .then(() => wordInfo.checkValidScrabble())
      //error to trigger if no successful response
      .catch(error => {
         console.log("API error: cannot contact the dictionary");
         API.displayError();
      })
   },
   displayError: function() {
      //display an error to the user on screen
      //"Sorry, cannot contact the dictionary at this time."
      document.getElementById("response-message").textContent = "Sorry, cannot contact the dictionary at this time.";
   }
}

var wordInfo = {
   
   //the current word under consideration
   word: '',
   //the raw xml response received via the API for this word
   apiResponse: null,
   isValidScrabble: null,
   
   checkWordNow: function() {
      //method to call the API for this word
      return API.callAPI(this.word);
   },
   checkValidScrabble: function() {
      //check the API response to see if it's a valid scrabble word
      if(this.apiResponse) {
         //pull the 0 or 1 value out of the xml
         this.isValidScrabble = this.apiResponse.getElementsByTagName("scrabble")[0].textContent;
         //check the value and display on screen
         document.getElementById("response-message").textContent = this.word + ": " + this.isValidScrabble;
      }
   },
   clearWord: function() {
      this.word = '';
      this.apiResponse = null;
      this.isValidScrabble = null;
      //also clear the display message
   }
}

var searchButton = document.getElementById("scrabble-search");
searchButton.addEventListener("click", function() {
   var searchInput = document.getElementById("scrabble-word").value;
   //run validation checks here
   //api will error if it receives punctuation in the word
   //if it passes validation:
   wordInfo.word = searchInput;
   wordInfo.checkWordNow();
});

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console