Skip to content Skip to sidebar Skip to footer

How To Print All Students Name Which Have Percentage More Than 70% In Javascript?

I am using json-rule-engine . https://www.npmjs.com/package/json-rules-engine I am having a student list which have name and their percentage, Also I have business rule the percen

Solution 1:

The json-rules-engine module takes data in a different format. In your Repl.it you have not defined any facts.

Facts should be:

let facts = [
  {
    name:"naveen",
    percentage:70
  },
  [...]

Also, the module itself doesn't seem to process an array of facts. You have to adapt it to achieve this. This can be done with:

facts.forEach((fact) => {
  engine
    .run(fact)
    [...]

Finally, the student data is found inside the almanac. You can get these values with: results.almanac.factMap.get('[name|percentage|age|school|etc]').value

Here is the updated Repl.it: https://repl.it/@adelriosantiago/json-rules-example

Solution 2:

Here is a working example.

Counting success and failed cases

const { Engine } = require("json-rules-engine");


let engine = newEngine();

const students = [
  {
    name:"naveen",
    percentage:70
  },
  {
    name:"rajat",
    percentage:50
  },
  {
    name:"ravi",
    percentage:75
  },
  {
    name:"kaushal",
    percentage:64
  },
  {
    name:"piush",
    percentage:89
  }
] 

engine.addRule({
    conditions: {
            all: [{
            fact: 'percentage',
            operator: 'greaterThanInclusive',
            value: 70
        }]
    },
    event: { type: 'procedure_result'}
})

let result = {success_count : 0 , failed_count : 0}

engine.on('success', () => result.success_count++)
    .on('failure', () => result.failed_count++)

const getResults = function(){
    returnnewPromise((resolve, reject) => {
        students.forEach(fact => {
            return engine.run(fact)
            .then(() =>resolve())
        })
    })
}

getResults().then(() =>console.log(result));

Post a Comment for "How To Print All Students Name Which Have Percentage More Than 70% In Javascript?"