-3

I have this function to compare to list in javaScript

function listCompare(list1, list2) {
    let result = 0;
    let final = (list1.length + list2.length) / 2;
    for (let x of list1) {
        for (let y of list2) {
            if (x == y) {
                result += 1;
            }
        }
    }
    return result / final * 100;
};

Its work just fine alone but when call it inside another function it return only zero. I don't know why this is the full code

function listCompare(list1, list2) {
    let result = 0;
    let final = (list1.length + list2.length) / 2;
    for (let x of list1) {
        for (let y of list2) {
            if (x == y) {
                result += 1;
            }
        }
    }
    return result / final * 100;
};

$('#id_password').change(function() {
    // console.log('changed')
    ls1 = time
    ls2 = ob
    var result = listCompare(ls1, ls2)
    console.log(result)
    if (result >= 70) {
    $(':button[type="submit"]').prop('disabled', false);
     }


});

note: I have two list one of them is time the author on is ob the function shode return the degree of matching between the two list and five to me

var test1 = [10,20,30,40,50,60,70,80];
var test2 = [10,20,30,40,50,60,70,80];
var test3 = [12,41,85,75,23,99,81,1236];
var test4 = [41,59,830,10,51,16,78,81];
console.log(listCompare(test1,test2))
output : 100
console.log(listCompare(test3,test4))
Output: 25
console.log(listCompare(test1,test4))
Output: 12.5
Bytech
  • 1,125
  • 7
  • 22
Mhadi Ahmed
  • 25
  • 1
  • 8

2 Answers2

0

thanks, everyone, the problem was one of the var return as a string so I hade to convert it to list you will find the answer here[enter link description here][1]

[][1]: Convert string with commas to array

Mhadi Ahmed
  • 25
  • 1
  • 8
0

I'd like to look at how many elements the time and ob variables contain at runtime. alert(time.length+','+ob.length);. I don't see you assigning any values to those variables in your $('#id_password').change function

Cirshiss
  • 81
  • 9