Skip to main content
Code Review

Return to Answer

Updated fiddlejs
Source Link
propagated the simplification
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478
 if (!Array.isArray(chars)) {
 if (typeof chars === 'string') {
 let tmp = [];
 tmp.push(chars);
 chars = tmp; 
 } //...
 }

That I changed intoWhat you want here is : "Return an array containing chars". What you do instead is : "Create an empty array. Push chars into it. Return the array". Then it becomes :

if (!Array.isArray(chars)) {
 if (typeof chars === 'string') {
 return [chars];
 }
}

What you want here is : "Return an array containing chars". What you do instead is : "Create an empty array. Push chars into it. Return the array".

EDIT : AsAlso, as 200_success said, validating that char is a string twiceand that it is not an array is redundant. I modified getSanizitedChars, so that it fits his answer.can be further simplified to :

if (!Array.isArray(chars)) {
 return [chars];
}
 if (!Array.isArray(chars)) {
 if (typeof chars === 'string') {
 let tmp = [];
 tmp.push(chars);
 chars = tmp; 
 } //...
 }

That I changed into :

if (!Array.isArray(chars)) {
 if (typeof chars === 'string') {
 return [chars];
 }
}

What you want here is : "Return an array containing chars". What you do instead is : "Create an empty array. Push chars into it. Return the array".

EDIT : As 200_success said, validating that char is a string twice is redundant. I modified getSanizitedChars so that it fits his answer.

 if (!Array.isArray(chars)) {
 if (typeof chars === 'string') {
 let tmp = [];
 tmp.push(chars);
 chars = tmp; 
 } //...
 }

What you want here is : "Return an array containing chars". What you do instead is : "Create an empty array. Push chars into it. Return the array". Then it becomes :

if (!Array.isArray(chars)) {
 if (typeof chars === 'string') {
 return [chars];
 }
}

Also, as 200_success said, validating that char is a string and that it is not an array is redundant, so it can be further simplified to :

if (!Array.isArray(chars)) {
 return [chars];
}
Improving the answer thanks to 200_success.
Source Link
function getIndexInAlphabet(chars) {
 var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''),
 validateItemIsString = function(item) {
 if (typeof item !== 'string') {
 throw new Error('Element ' + i +
 ' invalid because not of type string.');
 }
 },
 validateCharsAreStrings = function(chars) {
 chars.forEach(function(item, i) {
 validateItemIsString(item);
 });
 },
 getSanitizedChars = function(chars) {
 if (!Array.isArray(chars)) {
 if (typeof chars === 'string') {
  return= [chars];
 }
 throw new Error('Parameter invalid because not of type string.');
 }
 validateCharsAreStrings(chars);
 return chars;
 },
 getIndexInAlphabet = function(char, i) {
 var ret = alphabet.indexOf(char.toLowerCase()) + 1;
 if (ret === 0) {
 throw new Error('Element ' + i + ' invalid because' + ' not an alphabetic character.');
 }
 return ret;
 };
 return getSanitizedChars(chars).map(function(char, i) {
 return getIndexInAlphabet(char, i);
 });
}

What you want here is : "Return an array containing chars". What you do instead is : "Create an empty array. Push chars into it. Return the array".

EDIT : As 200_success said, validating that char is a string twice is redundant. I modified getSanizitedChars so that it fits his answer.

function getIndexInAlphabet(chars) {
 var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''),
 validateItemIsString = function(item) {
 if (typeof item !== 'string') {
 throw new Error('Element ' + i +
 ' invalid because not of type string.');
 }
 },
 validateCharsAreStrings = function(chars) {
 chars.forEach(function(item, i) {
 validateItemIsString(item);
 });
 },
 getSanitizedChars = function(chars) {
 if (!Array.isArray(chars)) {
 if (typeof chars === 'string') {
  return [chars];
 }
 throw new Error('Parameter invalid because not of type string.');
 }
 validateCharsAreStrings(chars);
 return chars;
 },
 getIndexInAlphabet = function(char, i) {
 var ret = alphabet.indexOf(char.toLowerCase()) + 1;
 if (ret === 0) {
 throw new Error('Element ' + i + ' invalid because' + ' not an alphabetic character.');
 }
 return ret;
 };
 return getSanitizedChars(chars).map(function(char, i) {
 return getIndexInAlphabet(char, i);
 });
}

What you want here is : "Return an array containing chars". What you do instead is : "Create an empty array. Push chars into it. Return the array".

function getIndexInAlphabet(chars) {
 var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''),
 validateItemIsString = function(item) {
 if (typeof item !== 'string') {
 throw new Error('Element ' + i +
 ' invalid because not of type string.');
 }
 },
 validateCharsAreStrings = function(chars) {
 chars.forEach(function(item, i) {
 validateItemIsString(item);
 });
 },
 getSanitizedChars = function(chars) {
 if (!Array.isArray(chars)) {
 chars = [chars];
 }
 validateCharsAreStrings(chars);
 return chars;
 },
 getIndexInAlphabet = function(char, i) {
 var ret = alphabet.indexOf(char.toLowerCase()) + 1;
 if (ret === 0) {
 throw new Error('Element ' + i + ' invalid because' + ' not an alphabetic character.');
 }
 return ret;
 };
 return getSanitizedChars(chars).map(function(char, i) {
 return getIndexInAlphabet(char, i);
 });
}

What you want here is : "Return an array containing chars". What you do instead is : "Create an empty array. Push chars into it. Return the array".

EDIT : As 200_success said, validating that char is a string twice is redundant. I modified getSanizitedChars so that it fits his answer.

Source Link
Loading
lang-js

AltStyle によって変換されたページ (->オリジナル) /