本文介绍了定价差异v2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我昨天问了这个问题,并根据我的指示修改了它.当列表中有项目时,它的工作方式与我想要的方式相同,但是当值为0时,它表示为false,因此or不会增加任何影响.我不希望当项目计数为0时我需要它显示0.00.我知道0 = false这就是原因或开始之处.有什么帮助/解决方法吗?

Okay so i asked this question yesterday and revised it based on the direction i was pointed. It works the way i want it when there are items in the list but, when there is 0 it evals to false so the or kicks in no mater what. I don't want that i need it to show 0.00 when the item count is at 0. I know that 0=false that's why or kicks in. Any help/workaround?

var dropResult;
$(function (){
    $(".t").click(function(){
        dropResult = $("input[name=drop]:checked").val();
        dropCalculate();
    });
});

function dropCalculate() {
    var pricesObj = {
        '0':0,
        '1':450
      };
        var dropAmount = $(".itemadd", "#items").length;
        $("#dropAmount").html("Total: " + dropAmount);
        if (dropResult == 1) {
            dropTotal = (pricesObj[dropAmount] * dropAmount) || 450 + (150 * dropAmount - 150);
            $("#dropPrice").html("Price Total: $" + dropTotal.toFixed(2));
        }else {
            dropTotal = (pricesObj[dropAmount] * dropAmount / 2) || 225 + (75 * dropAmount - 75);
            $("#dropPrice").html("Price Total: $" + dropTotal.toFixed(2));
        }
}

推荐答案

代码审查堆栈交换处在您没有直接问题但认为您可以改善代码设计的问题上.

You might get a better response over at the Code Review Stack Exchange on questions where you don't have an immediate problem, but feel that you could improve your code design.

以下代码应与示例中的代码具有相同的作用,但我认为它更具可读性.在这个用例中似乎不需要对象,但将来可能会有用.

The following code should do the same thing as the code in your example, but I think it's more readable. An object doesn't seem to be necessary for this use case, but may be useful in the future.

再次,我应用了 DRY原则,这次我删除了魔术数字(代码中没有立即明确含义的数字).

Again, I applied the DRY Principle, and this time I removed Magic Numbers(numbers in the code with no immediately clear meaning).

var dropResult;
$(function (){
    $(".t").click(function(){
        dropResult = $("input[name=drop]:checked").val();
        dropCalculate(450);
    });
});

function dropCalculate(fullPrice) {
        var halfPrice = fullPrice / 2,
            quarterPrice = halfPrice / 2,
            dropAmount = $(".itemadd", "#items").length,
            finalPrice = 0.00;

        if(dropAmount > 0){
          if (dropResult === 1) {
             finalPrice = fullPrice; //the rest of this line was not necessary, as dropAmount - 1 is always 0 here.
          } else {
             finalPrice = halfPrice + (quarterPrice * (dropAmount - 1));
          }
        }

        $("#dropAmount").html("Total: " + dropAmount);
        $("#dropPrice").html("Price Total: $" + finalPrice.toFixed(2));
}

这篇关于定价差异v2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:02