Urgent help needed creating javascript permutation and combinationfunction

I really need urgent help in creating a permutation and combination function in my form. In this form users will input a 4 digit number (tablename___numbers) and there is a checkbox named “Box” (tablename___box) when user click on the checkbox it will count the total number of permutation and combination of the input numbers (tablename___numbers). for exmaple 0001 (3 same digit) = 4, 1100 (2 same digit = 6) and so on…

Form Id 25
I have tried a few coding but fail to get it running Please help to see where I have gone wrong. Thanks in advance.

const tablename___box = tablename___numbers[0]

form_25.tablename___box.get('tablename___numbers').getValue()

function disableBox (thisid) {
  const tablename___box = jQuery.trim($('form_25.tablename___numbers' + thisid).val())

  if (no.length !== 4) {
    $('tablename___box ' + thisid).attr('disabled', true)
    $('tablename___box ' + thisid).prop('checked', false)
  } else {
    const combinationCount = checkDigit(no)
    $('tablename___box ' + thisid).val(combinationCount)
    $('tablename___box ' + thisid).attr('disabled', false)
  }
}

function checkDigit (val) {
  const b = form_25.tablename___box.get('tablename___numbers').getValue()
  let same = 0
  let kcount = 0
  let returnData = 1

  for (let i = 0; i < 4; i++) {
    kcount++
    for (let j = kcount; j < 4; j++) {
      if (val.charAt(i) === val.charAt(j)) {
        same++
      }
    }
  }

  if (same === 4) {
    returnData = 1
  } else if (same === 3) {
    returnData = 4
  } else if (same === 2) {
    returnData = 6
  } else if (same === 1) {
    returnData = 12
  } else {
    returnData = 24
  }

  return returnData
}

So… permutations and combinations are different things. There’s 4 permutations of 0001, but 1 combination of 4 items picking 4 of them…

I have no idea what this function does:
var b = form_25.tablename___box .get(‘tablename___numbers’).getValue();
Is b the value you think it should be?

Actually, nevermind, because you never use b in your code. So… hmm. Maybe you can tell me what it’s there for and why you’re using it?

What calls checkDigit in your code?

1 Like

checkDigit is the formula for calculating number of permutation and combination for example user enter 0001 in the number field and click the checkbox (“Box” ) it will check the digit entered in the number field and respond 4 because 3 same digit = 4 and when user enter 0011 it will return 6 because 2 same digit = 6 etc…

Form id is 25
Form element : number and box

Hi @hashbackend,

For future reference, you can select your pasted code and click on the </> icon in the menu at the top of the text editor and it will format it as a code block when you submit your post.

I have done it for you this time.

Just to note, I had your code in my editor and standardJS picked up on a few things

function disableBox (thisid) {
  // 'tablename___box' is assigned a value but never used
  const tablename___box = jQuery.trim($('form_25.tablename___numbers' + thisid).val())
  // 'no' is not defined. 
  // I don't know if this is a Global variable elsewhere in your code.
  if (no.length !== 4) {
  ...

A bit further down

function checkDigit (val) {
  // 'b' is assigned a value but never used.
  const b = form_25.tablename___box.get('tablename___numbers').getValue()
  ...

At this point, i think the issue is the definition of no in the disableBox command, as rpg_digital hinted at. checkDigit, if supplied with appropriate input, works correctly.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.