Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules
package-lock.json
187 changes: 186 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,189 @@ function add(a, b){
return a + b;
}

exports.add = add;
function longestString(stringsArray) {
let seenLengths = {};

let lengths = stringsArray.map(string => {
seenLengths[string.length]
? seenLengths[string.length] += 1
: seenLengths[string.length] = 1;
return string.length;
});

let maxLength = Math.max(...lengths);
let maxLengthIndex = lengths.indexOf(maxLength);

if (seenLengths[maxLength] > 1) return -1;

return stringsArray[maxLengthIndex];
}

function l337 (inputString) {
//if (!inputString) throw new Error ('No input specified');

let dictionary = {
i : 1,
l : 1,
z : 2,
e : 3,
a : 4,
s : 5,
g : 6,
t : 7,
y : 7,
b : 8,
q : 9,
o : '0'
};

return inputString.split('').map(x => dictionary[x] || x ).join('');
}

function uniqueStrings (stringArray) {
return stringArray.reduce((acc,string)=>{
if(acc.includes(string)) return acc
acc.push(string)
return acc
},[]);
}

function Developer (name, languages) {
this.name = name;
this.languages = languages;
}

Developer.prototype.learnLanguage = function (language) {
if (!this.languages.includes(language)) this.languages.push(language);
};

function Garden (plants) {
Object.assign(this,plants);
}

Garden.prototype.plant = function (plants) {
Object.keys(plants).forEach( newPlant => {
this[newPlant]
? this[newPlant] += plants[newPlant]
: this[newPlant] = plants[newPlant];
});
}

Garden.prototype.harvest = function (plants) {
Object.keys(plants).forEach( harvesting => {
if(this[harvesting]){
this[harvesting] -= plants[harvesting];
if (this[harvesting]< 1) delete this[harvesting];
}
});
};

stringsConcat= function(inputArray){
return inputArray.reduce((finalString, input)=>{
if( typeof input == 'string' ) finalString.push(input);
return finalString;
},[]).join(' ')
}

function negativeOnly (inputArray) {
return inputArray.reduce((output, input) => {
if (input < 0) output.push(input);
return output;
}, []);
}

function camelise (inputString) {
let inputArray = inputString.split(' ');

let firstWord = inputArray.shift();
let outputString = inputArray.map((word) =>{
return word.slice(0,1).toUpperCase() + word.slice(1);
}).join('');
return firstWord.slice(0,1).toLowerCase() + firstWord.slice(1) + outputString;
}

function merging (objectArray){
return objectArray.sort((x,y)=>{
return Object.keys(y).length - Object.keys(x).length
}).reduce((acc,obj)=>{
acc = Object.assign(acc,obj);
return acc;
},{})

}

function possibleValues(objectArray) {
return objectArray.reduce((acc, obj) => {
Object.keys(obj).forEach((objKey) => {
if (acc[objKey] && !acc[objKey].includes(obj[objKey])) {
acc[objKey].push(obj[objKey]);
} else if (!acc[objKey]) {
acc[objKey] = [ obj[objKey] ];
}
});

return acc;
}, {});
}

function isPrime(n){
if(n<2)return false;

let q= Math.floor(Math.sqrt(n));

for(let i =2; i<= q ; i++){
if(n%i===0) return false;
}
return true;
}

function Walker (direction='N') {
if (direction.match(/^[NSEW]$/i)) {
this.direction = direction;
} else {
this.direction = 'N';
console.log(`I don't understand ${direction}, defaulting to N`);
}

this.coords = [0,0];
this.pathTaken = [[0,0]];
}

Walker.prototype.walk = function (direction,steps){

direction= direction.toUpperCase();
this.direction=direction;

if(direction === 'N') {
this.coords[1] += steps;
}else if(direction === 'S') {
this.coords[1] -= steps;
}
else if(direction === 'E') {
this.coords[0] += steps;
}
else if(direction === 'W') {
this.coords[0] -= steps;
}

this.pathTaken.push([...this.coords])

}

Walker.prototype.pathTaken = function () {
return this.pathTaken;
}

module.exports.add = add;
module.exports.longestString = longestString;
module.exports.l337 = l337;
module.exports.merging = merging
module.exports.uniqueStrings = uniqueStrings;
module.exports.Developer = Developer;
module.exports.Garden = Garden;
module.exports.stringsConcat = stringsConcat;
module.exports.negativeOnly = negativeOnly;
module.exports.camelise = camelise;
module.exports.possibleValues = possibleValues;
module.exports.isPrime= isPrime
module.exports.Walker = Walker;
Loading