Skip to content

[Feature Contribution]: Changes in Code #5

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
63 changes: 43 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
/**
* Hello Matt!
* Just wanted to contribute to you work. Let me know if this is ok with you and you can merge it with you master
*
* 1. @start: will always have default value of 0
* 2. @end: will require validation on program start
* 3. changes made to accomodate ES6
* 4. returns empty array even if no arguments are fed when the module is invoked
*
* Cheers and have a nice day.
**/

module.exports = function newArray(start, end) {
var n0 = typeof start === 'number',
n1 = typeof end === 'number'

if (n0 && !n1) {
end = start
start = 0
} else if (!n0 && !n1) {
start = 0
end = 0
module.exports = (start = 0, end) => {
let startStatus = (!Number.isNaN(start) && typeof start === "number");
let endStatus = (!Number.isNaN(end) && typeof end === "number");

/* make sure we have the right value for start */
start = (!startStatus) ? 0 : start;
startStatus = true;

/* when end is a falsy value type */
if (startStatus && !endStatus) {
end = start;
start = 0;
}

/* we need to make sure that length value for initializing array is always 0 or positive */
const length = end - start;

if (length < 0) {
throw new Error("Difference between 'end' and 'start' must always be '0' or greater");
}
else {
/* initialize */
let arrayOfNumbers = new Array(length);

/* iteration to create array */
for (let i = 0, currentValue = start; i < length; ++i, ++currentValue) {
arrayOfNumbers[i] = currentValue;
}

start = start|0
end = end|0
var len = end-start
if (len<0)
throw new Error('array length must be positive')

var a = new Array(len)
for (var i=0, c=start; i<len; i++, c++)
a[i] = c
return a
}
/* return to caller */
return arrayOfNumbers;
}
}