I have 3 buttons that concatenate input text differently:
var myTxtArea = document.getElementById('KWarea');
myTxtArea.value = myTxtArea.value.replace(/^\s*|\s*$/g, '');
var lines = $('#KWarea').val().replace(/\*/g, '').split('\n');
$('#produce').click(function () {
var endString = "";
myTxtArea.value = myTxtArea.value.replace(/^\s*|\s*$/g, '');
var lines = $('#KWarea').val().replace(/\*/g, '').split('\n');
for (var i = 0; i < lines.length; ++i) {
endString += '"*' + $.trim(lines[i]) + '*"' + ',' + '"* ' + lines[i] + ' *"' + ',';
// console.log(lines[i]);
}
var trimmedStr = endString.slice(0, -1);
$('#result1').html("ctx.keywords MATCHES (" + trimmedStr + ")");
$('#Strlength').html('Total string length: ' + trimmedStr.length);
});
$('#produce2').click(function () {
var endString = "";
for (var i = 0; i < lines.length; ++i) {
endString += '"*' + $.trim(lines[i]) + '*"' + ',';
}
var trimmedStr = endString.slice(0, -1);
$('#result1').html("ctx.keywords MATCHES (" + trimmedStr + ")");
$('#Strlength').html('Total string length: ' + trimmedStr.length);
});
$('#produce3').click(function () {
var endString = "";
for (var i = 0; i < lines.length; ++i) {
endString += '"' + $.trim(lines[i]) + '"' + ',';
}
var trimmedStr = endString.slice(0, -1);
$('#result1').html("ctx.keywords MATCHES (" + trimmedStr + ")");
$('#Strlength').html('Total string length: ' + trimmedStr.length);
});
I would like to avoid repeating over and over the following:
$('#result1').html("ctx.keywords MATCHES (" + trimmedStr + ")");
$('#Strlength').html('Total string length: ' + trimmedStr.length);
But trimmedStr
depends on endString
and I can't refactor without ending up with some var undefined
Many thanks
It looks like a little bigger refactoring might be beneficial. Something along these lines?
http://jsfiddle.net/g8rsxmy3/5/