2016-02-20 79 views
1

我目前在學習如何編寫代碼,而且我在使用jQuery時遇到了一些困難。我試圖通過我正在做的這個課程來做這個計算器作爲一個項目,我碰到了一個碰撞。我不知道如何解決我的代碼問題:一個問題是操作不能正常工作,也沒有數字。我試着按照教程,並決定使用不同的按鈕來玩弄它,但我沒有得到期望的結果。這些數字應顯示在整個部分,操作員應該給出數學結果。建立一個基本的計算器

你可以看到我的HTML,jQuery的完整的代碼和JavaScript這裏 http://codepen.io/IrvingG/pen/adRrvW

$(document).ready(function() { 
    var testNumLength = function(number) { 
if (number.length > 9) { 
    totaldiv.text(number.substr(number.length - 9, 9)); 
    if (number.length > 15) { 
    number = ""; 
    totaldiv.text("Err"); 
    } 
} 
}; 
var number = ""; 
var newnumber = ""; 
var operator = ""; 
var totaldiv = $("#results"); 
totaldiv.text("0"); 

$(".numbers button").not("#operators, #equals, #clear").click(function() { 
    number += this.html(); 
    totaldiv.text(number); 
    testNumLength.text(number); 
    }); 
$("#operators").click(function() { 
    operator += this.html(); 
    newnumber = number; 
    number = ""; 
    totaldiv.text("0"); 
    }); 

$("#clear, #clear1").click(function() { 
    number = ""; 
    totaldiv.text("0"); 
    if ($(this).attr("id") === "#clear") { 
    newnumber = ";" 
    } 
}); 

$("#equals").click(function(){ 
    if (operator === "+"){ 
    number = (parseInt(number, 10) + parseInt(number, 10).toString(10)); 
    } else if (operator === "-"){ 
    number = (parseInt(number, 10) - parseInt(number, 10).toString(10)); 
} else if (operator === "/"){ 
    number = (parseInt(number, 10)/parseInt(number, 10).toString(10)); 
} else if (operator === "*"){ 
    number = (parseInt(number, 10) * parseInt(number, 10).toString(10)); 
} 

totaldiv.text(number); 
testNumLength(number); 
number=""; 
newnumber=""; 
}); 

});

+0

右此行之後:編號+ = this.html();添加console.log(typeof number);並看看結果。你真的獲得數字,或只是字符串? – Amir

+0

謝謝!我會試試這個。 – riv

回答

0

下面是代碼Calculator

$(document).ready(function() { 
    var testNumLength = function(number) { 
if (number.length > 9) { 
    totaldiv.text(number.substr(number.length - 9, 9)); 
    if (number.length > 15) { 
    number = ""; 
    totaldiv.text("Err"); 
    } 
} 
}; 
var result; 
var number = ""; 
var newnumber = ""; 
var operator = ""; 
var totaldiv = $("#results"); 
totaldiv.text("0"); 

$(".numbers button").not(".operator,#equals ,#clear").click(function() { 
    number += $(this).html(); 

    totaldiv.text(number); 
    testNumLength(number); 

    }); 
$(".operator").click(function() { 
    operator = $(this).html(); 

    newnumber = number; 
    number = ""; 
    totaldiv.text("0"); 

    }); 

$("#clear, #clear1").click(function() { 
    number = ""; 
    totaldiv.text("0"); 
    if ($(this).attr("id") === "#clear") { 
    newnumber = ";"; 
    } 
}); 

$("#equals").click(function(){ 
    if (operator === "+"){ 
    result=parseInt(newnumber, 10) + parseInt(number, 10); 
    number = result.toString(10); 

    } else if (operator === "-"){ 
    result=parseInt(newnumber, 10) - parseInt(number, 10); 

    number = result.toString(10); 
} else if (operator === "/"){ 
    result=parseInt(newnumber, 10)/parseInt(number, 10); 
    number = result.toString(10); 
} else if (operator === "*"){ 
result=parseInt(newnumber, 10) * parseInt(number, 10); 
    number = result.toString(10); 
} 

totaldiv.text(number); 
testNumLength(number); 
number=""; 
newnumber=""; 
}); 
}); 
+0

非常感謝你,我正在瀏覽代碼,看看有什麼不同,這對我有很大的幫助。 – riv