gpt4 book ai didi

javascript - Angular 。如何不重复相同的功能

转载 作者:可可西里 更新时间:2023-11-01 13:48:04 27 4
gpt4 key购买 nike

此函数有效,但如何避免为第二个变量键入相同的 for 循环并以某种方式只使用一个。尽量不要在这里重复你自己的方法。我需要使用数组吗?

JS:

var app=angular.module('xpCalc', []);
app.controller('xpCalculatorController', function($scope){
$scope.currentLevel=1;
$scope.currentXp=function(){
var output=0;
for (var thisLVL=1; thisLVL < $scope.currentLevel; thisLVL++) {
output += ( Math.floor ( thisLVL + 300 * Math.pow( 2, thisLVL / 7 ) ) / 4 );
}
return output;
};
$scope.desiredLevel=1;
$scope.desiredXp=function(){
var output=0;
for (var thisLVL=1; thisLVL < $scope.desiredLevel; thisLVL++) {
output += ( Math.floor ( thisLVL + 300 * Math.pow( 2, thisLVL / 7 ) ) / 4 );
}
return output;
};
});

HTML:

<h2>{{desiredXp()-currentXp()}}</h2> 

最佳答案

试试这个。

var app=angular.module('xpCalc', []);
app.controller('xpCalculatorController', function($scope){
$scope.currentLevel=1;
$scope.currentXp=InternalLoop($scope.currentLevel);
$scope.desiredLevel=1;
$scope.desiredXp=InternalLoop($scope.desiredLevel);

function InternalLoop(level){
var output=0;
for (var thisLVL=1; thisLVL < level; thisLVL++) {
output += ( Math.floor ( thisLVL + 300 * Math.pow( 2, thisLVL / 7 ) ) / 4 );
}
return output;
};

});

关于javascript - Angular 。如何不重复相同的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39161159/

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