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.

            
              const scrabbleWordValue = word => {
  
  // scrabble letter values
  const values = { a:1, b:3, c:3, d:2, e:1, f:4, g:2, h:4, i:1, j:8, k:5, l:1, m:3, n:1, o:1, p:3, q:10, r:1, s:1, t:1, u:1, v:4, w:4, x:8, y:4, z:10 };
  
  // Pre-requesites - check that the string fulfills a good pattern:
  //   - it must start with at least a letter
  //   - followed by have 0 to two asterisks
  //   - repeated at least one time
  //   - optionally at the end it can have a (t) or (d) modifier
  // if it doesn't follow that pattern (invalid or empty), return 0 points
  if(!word.match(/^([a-z][\*]{0,2})+(\([t|d]\))?$/gi)) {
    return 0;
  }
  
  // word modifier - calculate the multiplier that will be applied to the word:
  //   - 1 (default): the word will not change value
  //   - 2 (if it ends with '(d)'): the word will be multiplied by 2
  //   - 3 (if it ends with '(t)'): the word will be multiplied by 3
  // after calculating the word multiplier, update the word id applicable.
  let wordModifier = 1;
  if (word.indexOf("(") > 0) {
    wordModifier = word.indexOf("(t)") > 0 ? 3 : 2;
    word = word.slice(0,-3);
  }
  
  // calculate the addition of all letters
  //   - if the letter is an asterisk, use the previous letter
  //   - update the previous letter to current letter
  //   - add the value of the letter to the total
  let previous = 0
  let valueWord = word.split('')
                      .reduce((acc, curr) => {
                        if (curr == '*') curr = previous;
                        previous = curr;
                        return values[curr] + acc;
                      }, 0);

  // if the words has seven letters, add 50 extra points!
  const allLettersBonus = word.replace(/\*/g,'').length == 7 ? 50 : 0;
  
  // return the value of the word times the multiplier plus the bonus for using all letters
  return valueWord * wordModifier + allLettersBonus;
}

let word = "he**llo(d)";

console.log(`"${word}" will give you ${scrabbleWordValue(word)} points`);

            
          
!
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.

Console