JavaScript – Create Stylesheet

At times you may need to add a dynamically created stylesheet to a page via JavaScript. You can do just that with the following function:

/**
 * Creates a STYLE element with the specified content.
 * @param  {string} content  The stylesheet content.
 * @return {!Element}  The created STYLE element.
 */
function createStyleSheet(content) {
    var style = document.createElement('style');
    var styleSheet = style.styleSheet;
    if (styleSheet) {
        styleSheet.cssText = content;
    }
    else {
        style.appendChild(document.createTextNode(content));
    }
    style.type = 'text/css';
    return style;
}

It is important to note that the above function simply create the STYLE tag with the specified stylesheet content. It does not add it to the head or anywhere else in the document. Also, it is important to note that some versions of IE limit you to 32 stylesheets per page so multiple uses of this function are not encouraged unless you know your visitors will not use such a version of IE.

POW – Sequential Triad 24

Many people are familiar with the math game 24, but not many know about the sequential triad version. You must make each of the following sequences true by only inserting operators and without changing the order of the numbers:

0   1   2 = 24
1   2   3 = 24
2   3   4 = 24
3   4   5 = 24
4   5   6 = 24
5   6   7 = 24
6   7   8 = 24
7   8   9 = 24
8   9   0 = 24
9   0   1 = 24

You can use any of the following operators on the left-hand side of the equation, but the right-hand side of the equation must remain untouched:

  • Addition: +
  • Subtraction (and negation): +
  • Multiplication: ×
  • Division: /
  • Exponential: ^
  • Modulo: &
  • Absolute Value: abs(X)
  • Ceiling (round up): ceil(X)
  • Floor (round down): floor(X)
  • Square root: sqrt(X)
  • Factorial: X!
  • Parentheses: ( and )

Feel free to post your answers below or you can wait until the answers are posted next week. 😎