我正在尝试构建一个简单的页面,它将计算您的赌注并计算您的回报,就像投注商店一样。
到目前为止,我通过硬编码努力工作。但是正如你将在我的代码中看到的那样,我有机会在一个警报中通过,这很有效。我的问题是尝试将这个变量合并到我的html中,我得到" NaN"的输出,但我不知道为什么!我的尝试被注释掉了,但希望这是一个简单的修复,我无法看到!
我的HTML:
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<title></title>
<script type = "text/javascript"
src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$.getJSON("http://localhost:8888/json.json", function(result){
$.each(result, function(i, field){
//create an element and append data attribute to the element - in this case field name
el = $("<div class='bet-block'>" + field.name + field.odds.numerator+"/" + field.odds.denominator + " </div>").data("name", field.name).data("odds", field.odds.numerator+"/" + field.odds.denominator);
//append element to DOM
$(".bets").append( el );
})
//define a click handler for all new elements to display data in a div
$(document).on( "click", ".bet-block", function() {
$(".bet-name").text( $( this ).data("name") );
$(".bet-odds").text( $( this ).data("odds") );
});
});
function calculate_odds() {
var winnings = 0,
betting_amount = $(".stake").val();
var current_odds = $('.bet-odds').text();
// THIS ECHOS CORRECT ODDS
alert(current_odds);
winnings = (2/1) * betting_amount;
// winnings = (current_odds) * betting_amount;
$('.js-winnings').text(winnings);
}
$(document).ready(function() {
$(".stake").keyup(function(){
calculate_odds();
});
});
});
</script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-8">
<div class="bets">
<h1>All Bets</h1>
</div>
</div>
<div class="col-md-4">
<div class="slip">
<h1>My Bet Slip</h1>
<div class="new-bet-slip">
<p><span class="bet-name"></span> to win @ <span class="bet-odds"></span></p>
<input class="stake" type="number" name="betting-amount" min="1" max="5">
<div class="bet-return">
<span>£</span><span class="js-winnings">0.00</span>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
我的JSON:
[{"name":"name1","odds":{"numerator":10,"denominator":1}},[{"name":"name2","odds":{"numerator":5,"denominator":2}}, [{"name":"name3","odds":{"numerator":2,"denominator":1}}, ]
提前致谢!
答案 0 :(得分:1)
您输入的JSON数据格式不正确,请检查方括号,然后重试。此网站有助于实现此目的:https://jsonformatter.curiousconcept.com/
答案 1 :(得分:0)
我希望这可以帮助你...
$.getJSON("http://localhost:8888/json.json", function(result){
//parse the json & try
var data= jQuery.parseJSON(result);
$.each(data, function(i, field){
//do your action
console.log(field.name)
$.each(field.odds, function(k, level2){
console.log(level2.numerator,level2.denominator)
});
});
});
答案 2 :(得分:0)
正如@gujefers所指出的那样:你的current_odds
声明为:
var current_odds = $('.bet-odds').text();
返回string
,请参阅http://api.jquery.com/text/
因此,当您尝试对arithmetic operation
中的*
字符串winnings = (current_odds) * betting_amount;
进行NaN
时,您会收到parseInt
(非数字)错误。
所以安全的赌注是尝试将字符串值转换为int。 string
功能会将int
转换为var a = '1';
var b = parseInt(a);
console.log(typeof(a)); //this will return string
console.log(typeof(b)); //this will return number
例如尝试这是控制台:
library("NMF")
library("doParallel")
library("foreach")
#load vignette dataset and shorten it for time's sake
data(esGolub)
esGolub <- esGolub[1:200,]
#output matrix
Extract_Genes <- matrix()
#set cores
registerDoParallel(cores = 3)
#Loop NMF runs and extract features
foreach(i =1:4, .packages = "NMF") %dopar%{
i <- nmf(esGolub, 3, nrun = 1)
Extract_Genes <- extractFeatures(i, format = "list")
}