[Solved] How can I make my JavaScript code more DRY? [closed]

Assuming the myFunction series of functions aren’t used elsewhere, you’d do: var addChangeListener = function(elem, hexInput) { elem.onchange = function() { hexInput.value = elem.value; } }; const elements = { ‘colorpicker01′:’hexinput01’, ‘colorpicker02′:’hexinput02’, ‘colorpicker03′:’hexinput03’} for(elemName in elements) { const elem = document.getElementById(elemName); const hexInput = document.getElementById(elements[elemName]); addChangeListener(elem, hexInput); } And if you need to reuse the … Read more

[Solved] Finding regular expression with at least one repetition of each letter

You could find all substrings of length 4+, and then down select from those to find only the shortest possible combinations that contain one of each letter: s=”AAGTCCTAG” def get_shortest(s): l, b = len(s), set(‘ATCG’) options = [s[i:j+1] for i in range(l) for j in range(i,l) if (j+1)-i > 3] return [i for i in … Read more

[Solved] Repeating Characters in the Middle of a String

def repeat_middle(text): a, b = divmod(len(text) – 1, 2) middle = text[a:a + b + 1] exclamations=”!” * len(middle) return ‘{}{}{}’.format(exclamations, middle * len(text), exclamations) >>> print repeat_middle(“abMNcd”) !!MNMNMNMNMNMN!! >>> print repeat_middle(“abMcd”) !MMMMM! solved Repeating Characters in the Middle of a String