From f7e104973cbf78aee60ff6642c6bdccfed18e961 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Fri, 15 Dec 2023 18:14:02 +0530 Subject: [PATCH] avniproject/avni-client#1217 - fixed logic...added more scenarios from the excel --- src/health_modules/child/zScoreCalculator.js | 4 +- src/test/child/zScoreCalculatorTest.js | 41 ++++++++------------ 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/src/health_modules/child/zScoreCalculator.js b/src/health_modules/child/zScoreCalculator.js index c9ed1fb..dfca96e 100644 --- a/src/health_modules/child/zScoreCalculator.js +++ b/src/health_modules/child/zScoreCalculator.js @@ -35,7 +35,7 @@ const getHfaReference = (gender, ageInMonths) => { function calc_sd(reference, sd) { const {L, M, S} = reference; - return M * ((1 + L * S * sd) ^ (1 / L)); + return M * Math.pow(1 + (L * S * sd), (1 / L)); } function roundToOneDecimal(value) { @@ -58,7 +58,7 @@ const calculate = (value, reference) => { const sd3pos = calc_sd(reference, 3); const sd3neg = calc_sd(reference, -3); const sd23pos = sd3pos - calc_sd(reference, 2); - const sd23neg = calc_sd(reference, 2) - sd3neg; + const sd23neg = calc_sd(reference, -2) - sd3neg; let zScore = roundToOneDecimal((Math.pow(value / reference.M, reference.L) - 1) / (reference.S * reference.L)); diff --git a/src/test/child/zScoreCalculatorTest.js b/src/test/child/zScoreCalculatorTest.js index e1c3ac2..3328816 100644 --- a/src/test/child/zScoreCalculatorTest.js +++ b/src/test/child/zScoreCalculatorTest.js @@ -25,40 +25,31 @@ describe("zScoreCalculator", () => { }); it("calculates weight for age z-scores for boys and girls between 0 and 5", () => { - let zScores, today = new Date(); + const today = new Date(); individual.dateOfBirth = moment(today).subtract(1, 'month'); - zScores = zScore(individual, today, 1.2, 54.7); - assert.equal(zScores.wfa, -6.7); + // check for numbers outside -4 and 4 + assert.equal(zScore(individual, today, 1.2).wfa, -6.8); + assert.equal(zScore(individual, today, 4.5).wfa, 0); + assert.equal(zScore(individual, today, 5.1).wfa, 1); - zScores = zScore(individual, today, 4.5, 54.7); - assert.equal(zScores.wfa, 0); + individual.dateOfBirth = moment(today).subtract(26, 'month'); + assert.equal(zScore(individual, today, 7.0).wfa, -4.7); - zScores = zScore(individual, today, 5.1, 54.7); - assert.equal(zScores.wfa, 1); + individual.dateOfBirth = moment(today).subtract(26, 'month'); + assert.equal(zScore(individual, today, 6.3).wfa, -5.4); individual.gender = female; - //Notice this calculation may not exactly match the zscores as provided in the reference - zScores = zScore(individual, today, 2.7, 54.7); - assert.equal(zScores.wfa, -3.1); + individual.dateOfBirth = moment(today).subtract(1, 'month'); + assert.equal(zScore(individual, today, 2.7).wfa, -3.1); //Notice this calculation may not exactly match the zscores as provided in the reference - zScores = zScore(individual, today, 3.2, 54.7); - assert.equal(zScores.wfa, -1.9); - - zScores = zScore(individual, today, 4.2, 54.7); - assert.equal(zScores.wfa, 0); - - zScores = zScore(individual, today, 4.8, 54.7); - assert.equal(zScores.wfa, 1); - - zScores = zScore(individual, today, 5.5, 54.7); - assert.equal(zScores.wfa, 2); - - zScores = zScore(individual, today, 6.2, 54.7); - assert.equal(zScores.wfa, 3); - + assert.equal(zScore(individual, today, 3.2).wfa, -1.9); + assert.equal(zScore(individual, today, 4.2).wfa, 0); + assert.equal(zScore(individual, today, 4.8).wfa, 1); + assert.equal(zScore(individual, today, 5.5).wfa, 2); + assert.equal(zScore(individual, today, 6.2).wfa, 3); //2 year old girl individual.dateOfBirth = moment(today).subtract(24, 'month');