Skip to content

Instantly share code, notes, and snippets.

@debajyoti-thetaonelab
Last active August 30, 2022 11:48
Show Gist options
  • Select an option

  • Save debajyoti-thetaonelab/616a92bae6e3efd174d7832518edca4d to your computer and use it in GitHub Desktop.

Select an option

Save debajyoti-thetaonelab/616a92bae6e3efd174d7832518edca4d to your computer and use it in GitHub Desktop.
/*
* Edit and run this file using node.
* e.g. node test001-findMax.js
*/
function findMax(numbers) {
// TODO: write your code here
}
/********************************************************
* FRAMEWORK code, IGNORE the how it works part
*/
testFn(
'CASE 1',
findMax,
[1, 2, 3, 4, 5, 6, 7, 8, 9],
9
);
testFn(
'CASE 2',
findMax,
[10, 20, 30, 40, 50, 60, 70, 80, 90],
90
);
testFn(
'CASE 3',
findMax,
[11111, 2222, 333, 444, 5555, 66666, 77, 888, 99],
66666
);
testFn(
'CASE 4',
findMax,
[19999, 2999, 3999, 4999, 5999, 6999, 7999, 8999, 9999],
19999
);
console.log('**************************************************')
/********************************************************
* FRAMEWORK code, IGNORE!
*/
function trimIndent(str) {
let minIndent = Number.MAX_VALUE;
let indents = str.match(/^(\s+)/gm);
for (let indent of indents) {
indent = indent.replace(/^\n+/, '');
if (indent.length < minIndent) {
minIndent = indent.length;
}
}
const stripRegex = new RegExp(`^(\\ |\\t){${Math.max(0, minIndent)}}`, 'gm')
return str.replace(stripRegex, '');
}
function deepEqual(a, b) {
return JSON.stringify(a) === JSON.stringify(b);
}
function testFn(caseNo, fn, input, expected) {
try {
const output = fn(input);
if (!deepEqual(output, expected)) {
process.stdout.write(trimIndent(`
\u001b[31mFAIL: ${caseNo}\u001b[0m
Input: ${input}
Expected: ${expected}
---------------------
Output: ${output}
==============================
`));
} else {
process.stdout.write(trimIndent(`
\u001b[32;1mPASS: ${caseNo}\u001b[0m
Input: ${input}
Output: ${output}
==============================
`));
}
} catch (error) {
process.stdout.write(trimIndent(`
\u001b[31mERROR: ${error.message}\u001b[0m
Input: ${input}
Expected: ${expected}
---------------------
Got: ${error.stack}
==============================
`));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment