Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PZ #50

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

PZ #50

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions index.spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@

<script src="https://unpkg.com/chai/chai.js"></script>
<script src="https://unpkg.com/mocha/mocha.js"></script>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mocha.browser.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/chai.min.js"></script>

<script>
var assert = chai.assert;
var assert = chai.assert;
</script>
</head>
<body>
<div id="mocha"></div>


<script class="mocha-init">
mocha.setup('bdd');
mocha.checkLeaks();
</script>
<script src="./js/lib.js"></script>
<script src="./js/lib.spec.js"></script>
<script src="./js/lib.spec.js">
</script>
<script class="mocha-exec">
mocha.run();
</script>
</body>
</html>
</html>
20 changes: 10 additions & 10 deletions js/lib.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* This function must add two numbers and return sum of numbers
* @param a {number|string}
* @param b {number|string}
Expand All @@ -8,7 +8,7 @@ function sum(a, b) {
return Number(a) + Number(b)
}

/**
/**
* This function takes a number and raises it to a power
* @param x
* @param n
Expand All @@ -24,7 +24,7 @@ function pow(x, n) {
return result;
}

/**
/**
* This function calculate factorial of number
* @param n {number}
* @returns {number}
Expand All @@ -33,16 +33,16 @@ function factorial(n) {
return n ? n * factorial(n - 1) : 1;
};

/**
/**
* This function calculate Fibonacci sequence
* @param n
* @returns {*|number}
*/
function fibonacci(n) {
return (n > 2) ? fibonacci(n - 1) + fibonacci(n - 2) : 1;
return (n < 2) ? n : fibonacci(n - 1) + fibonacci(n - 2);
}

/**
/**
* This function must remove some element for array of string by name
* @param list {string[]}
* @param name {string}
Expand All @@ -58,7 +58,7 @@ function removeByName(list, name) {
return result;
}

/**
/**
* This function create counter
* @param currentCount {number}
* @returns {function(): number}
Expand All @@ -69,7 +69,7 @@ function makeCounter(currentCount) {
};
}

/**
/**
* This function create async timeout and return unixtime like timer Id
* @param time {number}
* @returns {number}
Expand All @@ -84,7 +84,7 @@ function getAsyncTimerId(time) {
return timerId
};

/**
/**
* This function return promise and multiply paraments
* @param x{number}
* @returns {Promise<number>}
Expand Down Expand Up @@ -124,5 +124,5 @@ function httpGet(url) {
xhr.send();
});

}

}
107 changes: 97 additions & 10 deletions js/lib.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
// const assert = require("assert");
// const {
// sum,
// pow,
// factorial,
// fibonacci,
// makeCounter,
// removeByName
// } = require("./lib");

describe('Test suite for testing lib.js', () => {
describe('Test suite for testing sum function', () => {
it('should return sum of two numbers', () => {
Expand All @@ -19,21 +29,98 @@ describe('Test suite for testing lib.js', () => {
});

describe('Test suite for testing pow function', () => {
it('should raising x to the n power', () => {
// Write your code here
it('should raise x to the power of n', () => {
assert.equal(pow(2, 3), 8);
assert.equal(pow(5, 2), 25);
assert.equal(pow(10, 0), 1);
});

it('should return 1 if n is 0', () => {
assert.equal(pow(2, 0), 1);
assert.equal(pow(5, 0), 1);
assert.equal(pow(10, 0), 1);
});

});
describe('factorial function', () => {
it('should calculate factorial of a positive number', () => {
assert.equal(factorial(0), 1);
assert.equal(factorial(1), 1);
assert.equal(factorial(2), 2);
assert.equal(factorial(3), 6);
assert.equal(factorial(4), 24);
assert.equal(factorial(5), 120);
});

describe('Test suite for testing removeByName function', () => {
it('should remove some element from array', () => {
// Write your code here
it('should return 1 for factorial of 0', () => {
assert.equal(factorial(0), 1);
});
});

it('should not remove element from array', () => {
// Write your code here
describe('fibonacci function', () => {
it('should return 0 for n = 0', () => {
assert.equal(fibonacci(0), 0);
});

it('should return 1 for n = 1', () => {
assert.equal(fibonacci(1), 1);
});

it('should return correct Fibonacci number for positive n', () => {
assert.equal(fibonacci(2), 1);
assert.equal(fibonacci(3), 2);
assert.equal(fibonacci(4), 3);
assert.equal(fibonacci(5), 5);
assert.equal(fibonacci(6), 8);
// Add more test cases as needed
});
});
});
});

describe('removeByName function', () => {
it('should remove the specified element from the array', () => {
const list = ['apple', 'banana', 'orange'];
const nameToRemove = 'banana';
const expectedResult = ['apple', 'orange'];

assert.deepEqual(removeByName(list, nameToRemove), expectedResult);
});

it('should return the original array if the element to remove is not found', () => {
const list = ['apple', 'banana', 'orange'];
const nameToRemove = 'grape';

assert.deepEqual(removeByName(list, nameToRemove), list);
});

it('should return an empty array if the input array is empty', () => {
const list = [];
const nameToRemove = 'apple';

assert.deepEqual(removeByName(list, nameToRemove), []);
});

it('should not modify the original array', () => {
const list = ['apple', 'banana', 'orange'];
const nameToRemove = 'banana';
removeByName(list, nameToRemove);

assert.deepEqual(list, ['apple', 'banana', 'orange']);
});
});

describe('makeCounter function', () => {
it('should return a function that increments the counter', () => {
const counter = makeCounter(0);
assert.equal(counter(), 0);
assert.equal(counter(), 1);
assert.equal(counter(), 2);
});

it('should return a function that increments the counter with initial value', () => {
const counter = makeCounter(5);
assert.equal(counter(), 5);
assert.equal(counter(), 6);
assert.equal(counter(), 7);
});
});
});
});