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
137 changes: 137 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,140 @@ function add(a, b){
}

exports.add = add;

// Q1 longest string
function longestString(array){
return array.reduce(function(a, b) {return a.length > b.length? a : b;});
}

// a starts as the first elements, you then compare 'a' with the next element 'b', if 'b' is bigger, 'b' becomes 'a'.

exports.longestString = longestString;

//Q2 l337
function replaceInstances(string){
const arr = string.split('');
arr.replace('i', '1');
arr.replace('l', '1');
arr.replace('e','3');
arr.replace('a','4');
arr.replace('s','5');
arr.replace('g','6');
arr.replace('t','7');
return arr;
}

exports.replaceInstances = replaceInstances;

//Q3 - Unique String
function uniqueStrings(array){
const newArr = array.filter(function(element, index){
return array.indexOf(element) == index;
});
return newArr;
}
exports.uniqueStrings = uniqueStrings;

// Q4 - developer languages
function Developer(name, languages){
this.name = name;
this.languages = languages;
}

// Q5 - inheritance
Developer.prototype.learnLanguage = function(language){
this.languages.push(language);
this.languages = uniqueStrings(this.languages);
}

exports.Developer = Developer;

//Q6 - Garden
function Garden(plants){
for(var prop in plants){
if (plants.hasOwnProperty(prop)){
this[prop] = plants[prop];
}
}
// Object.keys(plants).forEach((key) => {
// if(plants.hasOwnProperty(key)){
// this[key] = plants[key];
// }
// })
}
// exports.Garden = Garden;

//Q7 - extend
Garden.prototype.plant = function(newPlants){
// for (let key in newPlants){
// this[key] = newPlants[key];
// };
Object.keys(newPlants).forEach((key)=>{
this[key] = newPlants[key];
})
}
// exports.Garden = Garden;

//Q8 - harvest
Garden.prototype.harvest = function(plantHarvest){
Object.keys(plantHarvest).forEach((key) =>{
this[key] -= plantHarvest[key];
if (this[key] === 0){
delete this[key];
}
})
}
exports.Garden = Garden;

function concat(array){
const newArr = array.filter(function(el){
return typeof el !== 'number';
})
return newArr.join('');
}

exports.concat = concat;

//Q9 - negative numbers only
function negativeOnly(array){
const negArr = array.filter(function(num){
return num < 0;
})
return negArr;
}

exports.negativeOnly = negativeOnly;

//Q10 - camelise
function camelise(string){
const arr = string.split(' ');
const newArr = arr.map(function(str){
return str.charAt(0).toUpperCase() + str.slice(1)
})
.join('');
const newNewArr = newArr.charAt(0).toLowerCase() + newArr.slice(1);
return newNewArr;
}

exports.camelise = camelise;

// ----------- for comparison, oliver's code ------------
function camelise(string) {
return string
.split(" ")
.map(function(str, i) {
if (i === 0) return str.toLowerCase();
return str.charAt(0).toUpperCase() + str.slice(1);
})
.join("");
}
// exports.camelise = camelise;

//Q11 - merging
function merging(arr){
const sorted = arr.slice().sort(function(a,b){
return Object.keys(a).length < Object.keys(b).length;
})
return Object.assign({}, ...sorted);
}
exports.merging = merging;
81 changes: 81 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,84 @@ test('Addition', function(){
const result = functions.add(2, 3);
expect(result).toBe(expected);
});

//Q1 - longest string
test('Longest String', function(){
const expected = 'panda';
const result = functions.longestString(['cat', 'dog', 'bear', 'panda']);
expect(result).toBe(expected);
});

//Q2
test('l337', function(){
const expected = 'C0n57ruc70r';
const result = functions.replaceInstances("Constructor");
expect(result).toBe(expected);
});

// Q3 - unique string
test('Unique Strings', function(){
const expected = ['a','1','4','2'];
const result = functions.uniqueStrings(['a','1','4','4','2','a']);
expect(result).toEqual(expected);
})

test('Developer', function(){
const expected = {name: 'Harry', languages: ['Python', 'JavaScript', 'Ruby']};
const result = new functions.Developer('Harry', ['Python', 'JavaScript', 'Ruby']);
expect(result).toEqual(expected);
});

test('Learn Language', function(){
const expected = ['Python', 'JavaScript', 'Ruby', 'PHP'];
const phoebe = new functions.Developer('Phoebe', ['Python', 'JavaScript', 'Ruby']);
phoebe.learnLanguage('PHP');
const result = phoebe.languages;
expect(result).toEqual(expected);
});

test('Garden'), function(){
const expected = {tulip: 15, rosebush: 3, appletree: 1};
const result = new functions.Garden({tulip: 15, rosebush: 3, appletree: 1});
expect(result).toEqual(expected);
}

test('new plant', function(){
const expected = {tulip: 15, rosebush: 3, appletree: 1, poppy: 6};
const newGarden = new functions.Garden({tulip: 15, rosebush: 3, appletree: 1});
newGarden.plant({poppy: 6});
const result = newGarden;
expect(result).toEqual(expected);
});

test('Harvest', function(){
const expected = {tulip: 9, rosebush:1};
const newGarden = new functions.Garden({tulip: 15, rosebush: 3, appletree: 1});
newGarden.harvest({tulip: 6, rosebush: 2, appletree: 1});
const result = newGarden;
expect(result).toEqual(expected);
})

test('concat', function(){
const expected = 'constructorlabs';
const result = functions.concat(['constructor', 'labs', 1]);
expect(result).toEqual(expected);
})

test('negativeOnly', function(){
const expected = [-5, -10, -123];
const result = functions.negativeOnly([1, 3, -5, -10, 46, -123, 122]);
expect(result).toEqual(expected);
})

test('camelise', function(){
const expected = 'makeMeACamel';
const result = functions.camelise('make me a camel');
expect(result).toBe(expected);
})

test.only('merging', function(){
const expected = {a: 6, b: 12, c: 19} ;
const result = functions.merging([{a: 6}, {a: 7, b: 12}, {a: 2, b: 5, c: 19}]);
expect(result).toEqual(expected);
})