Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <p class="saidai">最大<span class="sale-price-max"></span>円OFFです!</p>


<section class="sale">
<p>なんと<span class="sale__sagaku"></span>円OFF!!!</p>
<p><span class="sale__teika">yen10,000aaa</span>円 -> <span class="sale__waribiki">aaa1,000</span>円</p>
</section>

<section class="sale">
<p>なんと<span class="sale__sagaku"></span>円OFF!!!</p>
<p><span class="sale__teika">aaa5,000,000</span>円 -> <span class="sale__waribiki">1,0aaa00</span>円</p>
</section>

              
            
!

CSS

              
                .saidai,
.sale {
  padding: 5px 20px;
}
              
            
!

JS

              
                
$(function() {

  // カンマをとる処理
  function removeComma(str) {
    return str.replace(/[^0-9]/g,"");
  }

  // 差額を計算する処理
  // 変な値はややこしいので0に変換する
  function calc_sagaku(val1, val2) {
    val1 = ( isNaN( parseInt(val1) ) )? 0: parseInt(val1);
    val2 = ( isNaN( parseInt(val2) ) )? 0: parseInt(val2);
    return (val1 - val2);
  }

  // カンマを入れる処理
  function addComma(str) {
    var num = new String(str);
    while(num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
    return num;
  };


  $(".sale").each(function(){
 
    var strTeika = $(this).find(".sale__teika").text();
    var strWaribiki = $(this).find(".sale__waribiki").text();

    // カンマをとる処理
    var numTeika = removeComma(strTeika);
    var numWaribiki = removeComma(strWaribiki);

    // 差額を計算する処理
    var sumPrice = calc_sagaku(numTeika, numWaribiki);

    // カンマを入れる処理
    var strSumPrice = addComma(sumPrice);
 
    $(this).find(".sale__sagaku").text(strSumPrice);
 
  });




  // ここから差額の最大金額を取得するjs

  var saleSagaku = $(".sale__sagaku");
  var salePriceMax = $(".sale-price-max");

  // class="sale__sagaku" の各要素から、テキストを取得して、配列に格納
  // 参考 http://www.task-notes.com/entry/20141019/1413717129
  var arrPrice = saleSagaku.map(function(){
    return $(this).text().replace(/[^0-9]/g,""); //数字だけにしたテキストを取り出す
  }).get();// 普通の配列にしたい場合は.get()メソッドを追加。.get()がないとjQueryでラップされた状態で排出され、不便な場合もあるらしい。

  // applyの第一引数は何でもよい。結果は変わらない。
  // Math.max.applyで最大値を出して、それに対してコンマをつける
  // 参考 https://www.softel.co.jp/blogs/tech/archives/1377
  var strCommaSalePriceMax = addComma( Math.max.apply(null, arrPrice) );

  salePriceMax.text( strCommaSalePriceMax );

  // ここまで差額の最大金額を取得するjs


});
              
            
!
999px

Console