0

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.

Larm
  • 187
  • 12
  • If you are combining two variable values, where is the code to check whether the variable value is set or not, in simple term where is the empty check? – Ramesh Aug 25 '18 at 04:58
  • Why don't you just use HTML5 input validation? – Maximilian Burszley Aug 25 '18 at 04:58
  • @Ramesh, that's what I'm trying to work out. Whether there's a way to do it during the .text() section or whether it needs to occur outside of that? – Larm Aug 25 '18 at 05:12
  • @TheIncorrigible1 because I like the additional features that comes with zxcvbn, like suggestion what a user might do to fix their weak password. I'll also probably include that for noscript – Larm Aug 25 '18 at 05:13
  • 1
    Yes it's possible.You can use **ternary** operator to do this.But if you ask me, I will suggest you to check the validation separately It's standard to your code. – Ramesh Aug 25 '18 at 05:15

1 Answers1

1

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);
JasonB
  • 6,243
  • 2
  • 17
  • 27