34
loading...
This website collects cookies to deliver better user experience
const riddlerChallenge01 = (items = []) => {
const options = [{ value: 'name' }];
const defaultName = items.find(val => val.isEnabled);
let output = '';
switch (true) {
case Boolean(defaultName):
output = 'defaultName';
break;
case options.length > 0:
output = options[options.length - 1].value;
break;
default:
break;
}
return output;
};
const riddlerChallenge01 = (items = []) => {
const options = [{ value: 'name' }]; // An array with a single item -> probably should be converted to a simpler variable
const defaultName = items.find(val => val.isEnabled); // Should use Array.some
let output = '';
switch (
true // Incredible that this passed a code review, with a fixed value the switch is the wrong construct
) {
case Boolean(defaultName): // The Boolean check should be written as defaultName !== unknown
output = 'defaultName'; //Convert to constant string
break;
case options.length > 0: //options.length > 0 => is always true
output = options[options.length - 1].value; //The array never changes - the string could be used directly
break;
default: //unneeded default case
//unreachable code
break;
}
return output;
};
it('works for no items', () => {
expect(riddlerChallenge01([])).toEqual('name');
});
it('works with an enabled item', () => {
expect(riddlerChallenge01([{ isEnabled: true }])).toEqual('defaultName');
});
it('works with an disabled item', () => {
expect(riddlerChallenge01([{ isEnabled: false }])).toEqual('name');
});
it('works with an mixed items', () => {
expect(riddlerChallenge01([{ isEnabled: true }, { isEnabled: false }])).toEqual('defaultName');
});
Boolean(defaultName)
. Depending on the input it is either true, and then the case gets executed. It does not execute the second case because of the break;
.Boolean(defaultName)
evaluates to false, then the switch case will always execute options.length > 0
as it always evaluates to true. This in turn means that the default-case cannot be reached and is not needed.const riddlerChallenge01 = (items = []) => {
const options = [{ value: 'name' }];
const defaultName = items.find(val => val.isEnabled);
let output = '';
if(defaultName !== undefined) {
output = 'defaultName';
} else {
output = options[options.length - 1].value;
}
return output;
};
options
probably had multiple values in the past, and is now just a hangover from an older version of the code. As the array only contains a single item and never gets modified > the array should be converted to a string.const riddlerChallenge01 = (items = []) => {
const defaultName = items.find(val => val.isEnabled);
let output = '';
if(defaultName !== undefined) {
output = 'defaultName';
} else {
output = 'name';
}
return output;
};
defaultName
is misleading as it indicates that it is a string but it used as an boolean. This in turn means that it is better to use Array.some()
that returns a boolean instead of Array.find()
that returns the object.output
to appTitle
to make it more clear why we are saving this string.const riddlerChallenge01 = (items = []) => {
let appTitle = 'name';
const useDefaultName = items.some(val => val.isEnabled);
if(useDefaultName) {
appTitle = 'defaultName';
}
return appTitle;
};
else
branch of the code as well. This is mostly to mimic the switch mechanism more closely. If you would want to extend it with another case then you would just add another if
block.