I think it is too odd when one have to use IF to return TRUE or FALSE... So I explictly converted the value to boolean and returned it, bypassing an unwanted if statement.
That said, I noticed that this does not cover when a property's value can be zero or FALSE.
As you consider '' as an unwanted value, I suppose the same goes for 0 and FALSE, which I think is an odd thing to assume.
function validateProperty(objectForValidate, specificProperty) { return Boolean(objectForValidate[specificProperty]); }
- function validateProperty(objectForValidate, specificProperty) {
if(objectForValidate[specificProperty]){return true;} else{return false;}- return Boolean(objectForValidate[specificProperty]);
- }
describe("Validate property in object", function(){ it("should be return true when the property exists", function(){ const test = { a: 2 }; const validation = validateProperty(test, 'a'); Test.assertEquals(validation, true); }); it("should be return false when the property not exists", function(){ const test = {}; const validation = validateProperty(test, 'a'); Test.assertEquals(validation, false); }); it("should be return false when the property is equals to empty", function(){ const test = { a: '' }; const validation = validateProperty(test, 'a'); Test.assertEquals(validation, false); }); it("should be return true when the property is equals to zero", function(){ const test = { a: '0' }; const validation = validateProperty(test, 'a'); Test.assertEquals(validation, true); }); it("should be return true when the property is equals to zero", function(){ const test = { a: 0 }; const validation = validateProperty(test, 'a'); Test.assertEquals(validation, false); }); it("should be return true when the property is equals to zero", function(){ const test = { a: false }; const validation = validateProperty(test, 'a'); Test.assertEquals(validation, false); }); });
- describe("Validate property in object", function(){
- it("should be return true when the property exists", function(){
- const test = { a: 2 };
- const validation = validateProperty(test, 'a');
- Test.assertEquals(validation, true);
- });
- it("should be return false when the property not exists", function(){
- const test = {};
- const validation = validateProperty(test, 'a');
- Test.assertEquals(validation, false);
- });
- it("should be return false when the property is equals to empty", function(){
- const test = { a: '' };
- const validation = validateProperty(test, 'a');
- Test.assertEquals(validation, false);
- });
- it("should be return true when the property is equals to zero", function(){
- const test = { a: '0' };
- const validation = validateProperty(test, 'a');
- Test.assertEquals(validation, true);
- });
- it("should be return true when the property is equals to zero", function(){
- const test = { a: 0 };
- const validation = validateProperty(test, 'a');
- Test.assertEquals(validation, false);
- });
- it("should be return true when the property is equals to zero", function(){
- const test = { a: false };
- const validation = validateProperty(test, 'a');
- Test.assertEquals(validation, false);
- });
- });
The original solution did not cover for empty string.
Also, you can remove completly the sum part from the reduce as to make it simpler to read.
const sum = (a,b) => a+b; const getSum = array => array.reduce(sum,0);
const getSum = array => array.reduce((acc, i) => acc + i)- const sum = (a,b) => a+b;
- const getSum = array => array.reduce(sum,0);
Test.assertEquals(getSum([5,9,4,1]), 19, "looks like its wrong."); Test.assertEquals(getSum([4,0,4,50,8]), 66, "looks like its wrong."); Test.assertEquals(getSum([5,9,4,1,9,1,1,1,4,10]), 45, "looks like its wrong."); Test.assertEquals(getSum([]),0, "Error on empty array");
- Test.assertEquals(getSum([5,9,4,1]), 19, "looks like its wrong.");
- Test.assertEquals(getSum([4,0,4,50,8]), 66, "looks like its wrong.");
- Test.assertEquals(getSum([5,9,4,1,9,1,1,1,4,10]), 45, "looks like its wrong.");
- Test.assertEquals(getSum([]),0, "Error on empty array");