gpt4 book ai didi

javascript - Angularjs 中的温度中位数

转载 作者:行者123 更新时间:2023-12-03 07:49:43 25 4
gpt4 key购买 nike

我是 AngularJS 的新手,正在尝试从基础开始学习。我想做的就是:1. 我希望能够通过输入一个值然后单击“添加”按钮来输入任意数量的温度记录。2.当我点击“获取介质温度”按钮时,我希望能够看到我输入的温度记录的中位数

我写过这样的东西:

<!DOCTYPE html>
<html ng-app="app">

<head>
<link data-require="bootstrap-css@*" data-semver="3.2.0" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
<link rel="stylesheet" href="style.css" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>

</head>

<body ng-controller="TemperatureCtrl">
<h1>Temperature Monitor</h1>
<form name="temp" novalidate>
<label for="temperture">Add Temperture:</label>
<input type="number" name="temperture" id="temperture" placeholder="Enter temperature here" required>
<button>Add</button>
</form>
<hr />
<section>
<button>Get Median Temperture</button>
<br>
Current Median:
<!-- display current median here-->
</section>

</body>

</html>

例如,如果我输入了类似 [5, 1, -7, 7, 8, 3] --> [-7, 1, 3, 5, 7, 8] = (3+5)/2 = 4` 应该是我应该得到的东西。

JavaScript 逻辑和一些帮助会很有帮助。谢谢!

最佳答案

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<script>
Array.prototype.max = function () {
return Math.max.apply(Math, this);
};

Array.prototype.min = function () {
return Math.min.apply(Math, this);
};

angular.module('App', []).controller(
'AppController',
[
'$scope',
function($scope) {
$scope.model = {
value: 0,
array: [0]
};

$scope.addValue = function () {
$scope.model.array.push($scope.model.value || 0);
};

$scope.getSomeCalcValue = function () {
return ($scope.model.array.max() + $scope.model.array.min()) / 2;
};
}
]
);
</script>
</head>
<body ng-app="App" ng-controller="AppController">
<h1>Values Monitor</h1>
<form name="temp">
<label for="value">Add value:</label>
<input ng-model="model.value" type="number" name="value" id="value" placeholder="Enter value here" />
<button ng-click="addValue()">Add</button>
</form>
<hr />
<section>
Array: {{model.array.join(' ')}}
<br />
Some calc value: {{getSomeCalcValue()}}
</section>
</body>
</html>

关于javascript - Angularjs 中的温度中位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35051405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com