how to print all students name which have percentage more than 70% in javascript?

Viewed 693

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 percentage should be greater thank or equal to than 70 . so I want to print all students name those have percentage more than 70

here is my code https://repl.it/repls/AlienatedLostEntropy#index.js

student list

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

rule

engine.addRule({
  conditions: {
    all: [
      {
        fact: "percentage",
        operator: "greaterThanInclusive",
        value: 70
      }
    ]
  },
  onSuccess(){
    console.log('on success called')
  },
  onFailure(){
    console.log('on failure called')
  },
  event: {
    type: "message",
    params: {
      data: "hello-world!"
    }
  }
});

code https://repl.it/repls/AlienatedLostEntropy#index.js any update

3 Answers

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

I might have submitted a completely unrelated answer, but here goes. Since the students object is an array, you could just loop through it and then use an if else statement.

for (let i = 0; i < students.length; i++) {
    if (students[i].percentage >= 70) {
        console.log(students[i].name);
    }
}

Sorry if this is incorrect!

Here is a working example.

Counting success and failed cases

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


let engine = new Engine();

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(){
    return new Promise((resolve, reject) => {
        students.forEach(fact => {
            return engine.run(fact)
            .then(() => resolve())
        })
    })
}

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

Related