I'm really hoping this isn't a duplicate, although certainly possible and I'm just not searching the right terms.
I'm working on my own password strength checker based on flyingcars version.
And I'm trying to work out a way to combine two variables (only if both exist).
Here's what I've got:
var thisVal = $('.password_input').val(),
thisStrength = zxcvbn(thisVal),
thisMeter = $('.password_strength_meter'),
thisLabel = $('.password-text'),
thisSuggestion = $('.password-text-suggest');
thisSuggestion.text(thisStrength.feedback.warning + ". " thisStrength.feedback.suggestions);
If
Would the best way really be to do multiple if statements? Or is there some way of inlining it to the .text() section?
I'm considering extending this further perhaps, by including the time it would take to crack:
thisSuggestion.text(thisStrength.feedback.warning + ". This password could be cracked in: " + thisStrength.crack_times_display['online_no_throttling_10_per_second'] + ". " + thisStrength.feedback.suggestions);
so hopefully multiple if statements can be avoided.
A quick and clean way to do this is to add all of the variables that might contain strings to an array. You can then filter out the blank or falsey values and join the remaining ones with your chosen delimiter.
const a = 'This will be there',
b = '',
c = null,
d = 'So will this';
let result = [a, b, c, d].filter(x => x).join('. ') + '.';
console.log(result);