Math error in angularjs calculation - javascript

I am calculating the values and dividing the total by variable value (9).for example:
$scope.wald_costs = Math.round($scope.logistics_costs + $scope.refurbishment_costs + $scope.insurance_costs + $scope.priceful + $scope.t / $scope.$scope.sharable_with)
which will be
$scope.wald_costs = Math.round(1000 + 500 + 4500 + 45000 + 27000 / 9)
suppose to be $scope.wald_costs = 8666
but i am not getting the proper output when i print <b>{{wald_costs}}</b>.
Instead what i get in output is like this 6000450003375
$scope.t = 27000
$scope.logistics_costs = 1000
$scope.refurbishment_costs = 500
$scope.insurance_costs = 4500
$scope.priceful = 45000
$scope.sharable_with = 9
$scope.wald_costs = Math.round(($scope.logistics_costs + $scope.refurbishment_costs + $scope.insurance_costs + $scope.priceful + $scope.t / $scope.$scope.sharable_with))
how can i solve this?

Try this http://plnkr.co/edit/8ZZY2XtxB2EbB64hWYyn?p=preview
$scope.t = 27000;
$scope.logistics_costs = 1000;
$scope.refurbishment_costs = 500;
$scope.insurance_costs = 4500;
$scope.priceful = 45000;
$scope.sharable_with = 9;
$scope.wald_costs = Math.round(($scope.logistics_costs + $scope.refurbishment_costs + $scope.insurance_costs + $scope.priceful + $scope.t) / $scope.sharable_with);
alert($scope.wald_costs);

use
$scope.wald_costs = Math.round(($scope.logistics_costs + $scope.refurbishment_costs + $scope.insurance_costs + $scope.priceful + $scope.t) / $scope.$scope.sharable_with)

Related

Unit testing: Document is not defined, error for 'document.getElementById()'

I am new to javascript. I am doing a unit test on my project, and getting some error. Here is my code:
function timeConvert() {
let n = document.getElementById('yourNo').value;
var Jam = Math.floor(n / 3600);
var Menit = Math.floor(n / 60);
var Detik = Math.floor(n % 60);
document.getElementById("result")
.innerHTML = "Waktu yang tersisa: " + "<br>"
+ Jam + " Jam " + "<br>"
+ Menit + " Menit " + "<br>"
+ Detik + " Detik ";
}
console.log("normal use", timeConvert(1633790418));
And i got error like this:
C:\Users\USER\Desktop\backend_test\backend.js:3
let n = document.getElementById('yourNo').value;
^

how to get value from select without refresh?

I made a CGPA-SGPA calculator and I cannot get the value with refresh.
When I refresh and check the value it get feed
but not getting value without refresh.
var credit_1001 = document.querySelector('#credit_1001')
var grade_1001 = document.querySelector('#grade_1001')
var credit_1002 = document.querySelector('#credit_1002')
var grade_1002 = document.querySelector('#grade_1002')
var credit_1003 = document.querySelector('#credit_1003')
var grade_1003 = document.querySelector('#grade_1003')
var credit_1004 = document.querySelector('#credit_1004')
var grade_1004 = document.querySelector('#grade_1004')
var credit_1008 = document.querySelector('#credit_1008')
var grade_1008 = document.querySelector('#grade_1008')
var credit_1009 = document.querySelector('#credit_1009')
var grade_1009 = document.querySelector('#grade_1009')
//btn and result S1
var btn_check = document.querySelector('#check_s1')
var result_s1 = document.querySelector('#result_s1')
//btn click S1
var total_s1_credit = parseInt(credit_1001.value) + parseInt(credit_1002.value) + parseInt(credit_1003.value) + parseInt(credit_1004.value) + parseInt(credit_1008.value) + parseInt(credit_1009.value)
var got_s1_marks = parseInt(credit_1001.value) * parseInt(grade_1001.value) + parseInt(credit_1002.value) * parseInt(grade_1002.value) + parseInt(credit_1003.value) * parseInt(grade_1003.value) + parseInt(credit_1004.value) * parseInt(grade_1004.value) + parseInt(credit_1008.value) * parseInt(grade_1008.value) + parseInt(credit_1009.value) * parseInt(grade_1009.value)
btn_check.addEventListener('click', () => {
result_s1.innerText = " S1 SGPA " + got_s1_marks / total_s1_credit
console.log("S1 Total Credit = " + total_s1_credit)
console.log("S1 Got Marks = " + got_s1_marks)
console.log("S1 SGPA = " + result_s1.innerText)
})
You need to get the values for your calculation inside of the click handler, right now you get them before the click, and then use (the old values) when the click happens
btn_check.addEventListener('click', () => {
//btn click S1
var total_s1_credit = parseInt(credit_1001.value) + parseInt(credit_1002.value) + parseInt(credit_1003.value) + parseInt(credit_1004.value) + parseInt(credit_1008.value) + parseInt(credit_1009.value)
var got_s1_marks = parseInt(credit_1001.value) * parseInt(grade_1001.value) + parseInt(credit_1002.value) * parseInt(grade_1002.value) + parseInt(credit_1003.value) * parseInt(grade_1003.value) + parseInt(credit_1004.value) * parseInt(grade_1004.value) + parseInt(credit_1008.value) * parseInt(grade_1008.value) + parseInt(credit_1009.value) * parseInt(grade_1009.value)
result_s1.innerText = " S1 SGPA " + got_s1_marks / total_s1_credit
console.log("S1 Total Credit = " + total_s1_credit)
console.log("S1 Got Marks = " + got_s1_marks)
console.log("S1 SGPA = " + result_s1.innerText)
})

How to calculate `lib.ema` starting from the previous period?

Hi I need to calculate EMA in Zenbot, but starting from the previous period, how does one do that?
return function ema (s, key, length, source_key) {
if (!source_key) source_key = 'close'
if (s.lookback.length >= length) {
var prev_ema = s.lookback[0][key]
if (typeof prev_ema === 'undefined' || isNaN(prev_ema)) {
var sum = 0
s.lookback.slice(0, length).forEach(function (period) {
sum += period[source_key]
})
prev_ema = sum / length
}
var multiplier = 2 / (length + 1)
s.period[key] = (s.period[source_key] - prev_ema) * multiplier + prev_ema
}
}
}
Recently figured out the same thing, you can use this commit as a reference.
+++ b/extensions/strategies/wavetrend/strategy.js
## -0,0 +1,172 ##
...
+ onPeriod: function (s, cb) {
+ if (s.period.wto) {
+ s.signal = null // hold
+ let prev_wto = s.lookback[0].wto
+ let wto = s.period.wto
+ let prev_hcl3 = s.lookback[0].hcl3
+ let hcl3 = s.period.hcl3
+ let prev_ema = s.lookback[0].ema
+ let ema = s.period.ema
+
+ if (!s.sell_signal_close)
+ s.sell_signal_close = 0
+ if (!s.buy_signal_close)
+ s.buy_signal_close = 0
+ if (!s.sell_pct_orig)
+ s.sell_pct_orig = s.sell_pct
+ if (!s.buy_pct_orig)
+ s.buy_pct_orig = s.sell_pct

Javascript unexpected identifier with global variable

I had this code on my page:
<script type="text/javascript">
var timeFinal = 3;
function timeZone(timeBegin,timeEnd) {
var tz = new Date();
tz = tz.getTimezoneOffset() / 60;
timeBegin = timeBegin - tz;
if (timeBegin <= -1) {
timeBegin = 24 - timeBegin;
}
else {
}
timeFinal = timeBegin + ":" timeEnd;
}
function FileModifDate() {
var dateModif = document.lastModified;
var startTimeModif = dateModif.indexOf(":") - 2;
var time = dateModif.substring(startTimeModif, dateModif.length - 3);
var timeBegin = time.substring(0, 1);
var timeEnd = time.substring(3, 4);
var date = dateModif.substring(0, 6);
date = date.substring(3, 4) + "/" + date.substring(0, 1) + "/" + date.substring(8, 9);
timeZone(timeBegin,timeEnd);
dateModif = timeFinal + " " + date;
document.getElementById('ModifDate').innerHTMl = dateModif;
}
</script>
But for some reason an error happens. It says Unexpected Identifier in the line 21:
timeFinal = timeBegin + ":" timeEnd;
Can someone tell me what is wrong?
timeFinal = timeBegin + ":" + timeEnd;
----------------------------------------------^ you missed + here
fixed the code here

How do I "uniquely" repeat a function n times in javascript?

so I have some code that draws a path and a circle. the circle is animated across the path every single time the function is initiated. I simply want to create 10 paths, each with their own circle that animates across each path. When I simply execute the function 10 times, the paths are generated fine, however the circle all animate along the same, single path. what am I doing wrong here? Is the best method here to create a for(i=0) loop?
You can view my code on jfiddle, or take a look at it here:
function commerce() {
Raphael("commercebounce", function () {
var r = this;
function pathfade() {
var pa = .1,
pb = .4,
pc = [0, 2],
pd = [50, 300],
pe = [150, 800],
pf = [150, 350],
pg = pd[0] + Math.random() * (pd[1] - pd[0]),
ph = pe[0] + Math.random() * (pe[1] - pe[0]),
pi = pf[0] + Math.random() * (pf[1] - pf[0]),
bd = .1,
be = .9,
pathspot = bd + Math.random() * (be - bd),
colours = ["215,10,45", "115,115,115"],
stroke = ["", "- "];
opacity = pa + Math.random() * (pb - pa), colour = "rgb(" + colours[Math.round(Math.random())] + ")", strokeW = pc[Math.round(Math.random())];
pj = r.path("M 0 " + pg + " C 0 " + pg + " " + (ph - 100) + " " + pg + " " + ph + " 400 M " + ph + " 400 C " + ph + " 400 " + (ph + 100) + " " + pg + " 960 " + pi).attr({stroke: colour,"stroke-dasharray": stroke[Math.round(Math.random())],"opacity": 0});
bh = r.circle(0, 0, 7, 7).attr({"stroke-width": this.strokeW,stroke: colour,"stroke-opacity": this.opacity,fill: "none","fill-opacity": 0}).onAnimation(function() {
var t = this.attr("transform")})
leng = pj.getTotalLength();
r.customAttributes.along1 = function (v) {
var point = pj.getPointAtLength(v * leng);
return {
transform: "t" + [point.x, point.y] + "r" + point.alpha
}
};
return bh.attr({along1:0}), bh.animate({along1:pathspot},300), pj.animate({opacity:opacity},300), pj, bh
}
pathfade();//how do i repeat this function n times?
});
}
commerce();
As far as I can see a simple loop should be fine. If you're interested this is the fastest way to loop in JavaScript:
var i = 10; while (i--) {
//Your code..
}
You need to break the pathfade() function into multiple simple functions that do only a few task.
There are two main problems.
First, you're placing a semi-colon where there should be a comma during your variable declarations. Look at the stroke variable.
Second, pathfade is returning multiple values when javascript only supports one. Remember that once you return from a function the rest of the statements don't get executed.
And lastly, use a for, do-while, or while loop to repeat your function calls.
Here's the fix. Sorry I didn't have time to refactor.
function commerce() {
Raphael("commercebounce", function () {
var r = this;
function pathfade() {
var pa = 0.5,
pb = 0.9,
pc = [0, 2],
pd = [50, 300],
pe = [150, 800],
pf = [150, 350],
pg = pd[0] + Math.random() * (pd[1] - pd[0]),
ph = pe[0] + Math.random() * (pe[1] - pe[0]),
pi = pf[0] + Math.random() * (pf[1] - pf[0]),
bd = 0.1,
be = 0.9,
pathspot = bd + Math.random() * (be - bd),
colours = ["215,10,45", "115,115,115"],
stroke = ["", "- "],
opacity = pa + Math.random() * (pb - pa),
colour = "rgb(" + colours[Math.round(Math.random())] + ")",
strokeW = pc[Math.round(Math.random())],
pj = r.path("M 0 " + pg + " C 0 " + pg + " " + (ph - 100) + " " + pg + " " + ph + " 400 M " + ph + " 400 C " + ph + " 400 " + (ph + 100) + " " + pg + " 960 " + pi).attr({
stroke : colour,
"stroke-dasharray" : stroke[Math.round(Math.random())],
"opacity" : 0
}),
bh = r.circle(0, 0, 7, 7).attr({
"stroke-width" : this.strokeW,
stroke : colour,
"stroke-opacity" : this.opacity,
fill : "none",
"fill-opacity" : 0
}).onAnimation(function () {
var t = this.attr("transform")
}),
leng = pj.getTotalLength();
r.customAttributes.along1 = function (v) {
var point = pj.getPointAtLength(v * leng);
return {
transform : "t" + [point.x, point.y] + "r" + point.alpha
}
};
bh.attr({
along1 : 0
});
bh.animate({
along1 : pathspot
}, 300);
pj.animate({
opacity : opacity
}, 300);
}
var i = 10;
while( i-- ){
pathfade();
}
});
}
commerce();
Demo: http://jsfiddle.net/VEdwG/6/
Your should read "The Elements of C# Style".
http://www.amazon.com/The-Elements-Style-Kenneth-Baldwin/dp/0521671590/ref=cm_cr-mr-title
var n = 5; //or however much you want
for (var i = 0; i < n; i++){
pathfade();
}
The for loop method is the simplest option here.

Categories