Given list of students’ records. Write a program with signature as below:
Method Name: getStudentsWithMarksMoreThan()
Input: marks – Integer
Output: List of Students having marks more than the input
Also, add the Unit Test case – both positive and negative. (Any high-level language like – Java,
Python, PHP, C#, JavaScript)
//script.js
const input = document.querySelector('.form__input');
const btn = document.querySelector('.form__btn');
const textarea = document.querySelector('.textarea');
const list1 = {
'student1': 1,
'student2': 2,
'student3': 3,
'student4': 4,
'student5': 5,
'student6': 6,
'student7': 7,
'student8': 8,
'student9': 9,
'student10': 10,
};
function getStudentsWithMarksMoreThan(listStudents, inputValue = input.value) {
let result = {};
if (inputValue !== '' && !isNaN(inputValue)){
textarea.value = '';
for (const student in listStudents) {
if (listStudents[student] > inputValue) {
textarea.value += `${student}: ${listStudents[student]} \n`;
result[student] = listStudents[student];
}
}
if (textarea.value == '') {
textarea.value = "No one students having marks more than the input"
}
} else{
textarea.value = "Please write a mark number"
}
return result // if we want to work with result
}
btn.addEventListener("click", function () { getStudentsWithMarksMoreThan(list1)}); // start ower function
// Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.js"></script>
<script>
mocha.setup('bdd');
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.js"></script>
<script>
let assert = chai.assert;
</script>
<title>Document</title>
</head>
<body>
<div class="container">
<div class="form">
<label class="labal" for="form__input">Input mark</label>
<div class="form__group">
<input class="form__input" type="text">
<button class="form__btn">Get marks</button>
</div>
<textarea class="textarea" readonly></textarea>
</div>
<div id="mocha"></div>
</div>
<script src="script.js"></script>
<script src="test.js"></script>
<script>
mocha.run();
</script>
</body>
</html>
// Unit test js
describe("getStudentsWithMarksMoreThan", function () {
it("get Students with marks more than value 5", function () {
assert.deepEqual(getStudentsWithMarksMoreThan(
{
'student1': 1,
'student2': 0,
'student3': 3,
'student4': 4,
'student5': 5,
'student6': 2,
'student7': 7,
'student8': 8,
'student9': 1,
'student10': 6,
}
, 5),
{
'student7': 7,
'student8': 8,
'student10': 6,
});
});
it("get Students with marks more than value 2", function () {
assert.deepEqual(getStudentsWithMarksMoreThan(
{
'student1': 1,
'student2': 0,
'student3': 3,
'student4': 4,
'student5': 5,
'student6': 2,
'student7': 7,
'student8': 8,
'student9': 1,
'student10': 6,
}
, 2),
{
'student3': 3,
'student4': 4,
'student5': 5,
'student7': 7,
'student8': 8,
'student10': 6,
});
});
it("get Students with marks more than value 22", function () {
assert.deepEqual(getStudentsWithMarksMoreThan(
{
'student1': 1,
'student2': 0,
'student3': 3,
'student4': 4,
'student5': 5,
'student6': 2,
'student7': 7,
'student8': 8,
'student9': 1,
'student10': 6,
}
, 22),
{
'student3': 3,
'student4': 4,
'student5': 5,
'student7': 7,
'student8': 8,
'student10': 6,
});
});
});
Comments
Leave a comment