I am trying to do a test where this calculator app will return a false and show an alert message when invalid entries are given. However, I keep getting this error message:
1 failing
1) checkNum function
Test 3: should return false for a string with invalid entries:
ReferenceError: alert is not defined
at checkNum (test\test.js:78:5)
at Context.<anonymous> (test\test.js:58:24)
at process.processImmediate (node:internal/timers:478:21)
The following is my code. How do I resolve this issue? Thank you.
var assert = require('assert');
describe('checkNum function',()=>{
it('Test 3: should return false for a string with invalid entries', () => {
const result = checkNum('abc$def');
assert.equal(result, false);
});
});
function checkNum(str) {
for (var i = 0; i < str.length; i++) {
var ch = str.charAt(i);
if (ch < "0" || ch > "9") {
if (ch != "/" && ch != "*" && ch != "+" && ch != "-" && ch != "."
&& ch != "(" && ch!= ")" && ch != "%") {
alert("invalid entry!")
return false
}
}
}
return true
}
[1]: https://i.stack.imgur.com/MKiDi.png
>Solution :
This error occurred because Node.js was unable to execute alert,
the alert() is a method on the window object, which is only available in the browser
use console.log