-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
143 lines (105 loc) · 3.38 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//jshint esversion:6
const express = require('express');
const mathjs = require('mathjs');
const bodyParser = require("body-parser");
const ejs = require("ejs");
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res) {
res.render('dashboard', {outcomeResult: outcomeRes, result: expressionsResults});
outcomeRes = "";
expressionsResults = "";
});
/***********************************************************************
* Task-1
* Simulation of an Event that Follows Given Biasness
***********************************************************************/
function generateOutcome(outcomes) {
const randomNum = Math.random() * 100; // Generate a random number between 0 and 100
let cumulativeProb = 0;
for (const outcome of outcomes) {
cumulativeProb += outcome.probability;
if (randomNum <= cumulativeProb) {
return outcome.outcome;
}
}
return null; // Shouldn't happen if probabilities are correct
}
//it initialzes all outcomes with val zero
function initializeOccObj() {
for (const outcome of eventOutcomes) {
occurrenceCount[outcome.outcome] = 0;
}
}
// Example usage
const eventOutcomes = [
{ outcome: 'A', probability: 30 },
{ outcome: 'B', probability: 40 },
{ outcome: 'C', probability: 20 },
{ outcome: 'D', probability: 10 }
];
const numOccurrences = 1000;
// Generate outcomes for the given number of occurrences
const occurrenceCount = {};
initializeOccObj();
let outcomeRes = "";
app.get('/getoutcome', function(req, res) {
outcomeRes = "";
initializeOccObj();
for (let i = 0; i < numOccurrences; i++) {
const outcome = generateOutcome(eventOutcomes);
occurrenceCount[outcome]++;
}
// Print the occurrence count for each outcome
for (const [outcome, count] of Object.entries(occurrenceCount)) {
console.log(`On triggering the event ${numOccurrences} times, ${outcome} appeared ${count} times`);
outcomeRes += `On triggering the event ${numOccurrences} times, ${outcome} appeared ${count} times \n`;
}
res.redirect('/')
});
/***********************************************************************
* Task-2
* Evaluate multiple mathematical expressions at once using a Web API
* Api name: MathJS library
***********************************************************************/
//this method calls the evaluate function of MathJS library by passing expression as an argument
function evaluateExpression(expression) {
try {
return mathjs.evaluate(expression);
} catch (error) {
return 'Error: ' + error.message;
}
}
// Example expressions
const expressions = [
'2 * 4 * 4',
'5 / (7 - 5)',
'sqrt(5^2 - 4^2)',
'sqrt(-3^2 - 4^2)',
'log(10)',
'sin(0.5)',
'end'
];
let expressionsResults = ""; //results to display at UI
app.get('/evaluate', function(req, res) {
expressionsResults = "";
// Evaluate each expression and display the result on console
expressions.forEach((expression) => {
if (expression === 'end') {
return; // Skip the 'end' expression
}
const result = evaluateExpression(expression);
console.log(expression + ' = ' + result);
expressionsResults += expression + ' = ' + result + ',\n';
});
res.redirect('/');
});
let port = process.env.PORT;
if(port == null || port == "") {
port = 3000;
}
app.listen(port);