-2

I had tried this piece of code in java which is working properly fine but while switching in javascript it is not working properly.

function checkNumberIfContainsKey(number, key){
    while(number > 0){
        if(number%10 == key){
            return true;
        }
        number /= 10;        
    }
    return false;
}

console.log(checkNumberIfContainsKey(19, 9));
console.log(checkNumberIfContainsKey(191, 9));
console.log(checkNumberIfContainsKey(912, 9));
console.log(checkNumberIfContainsKey(854, 9));

this function should return true if it contains key at any position. example: checkNumberIfContainsKey(19, 9) output: true

my expected output:
checkNumberIfContainsKey(19, 9)   //true
checkNumberIfContainsKey(191, 9)  //true
checkNumberIfContainsKey(912, 9)  //true
checkNumberIfContainsKey(185, 9)  //false
my output:
checkNumberIfContainsKey(19, 9)   //true
checkNumberIfContainsKey(191, 9)  //false
checkNumberIfContainsKey(912, 9)  //false
checkNumberIfContainsKey(185, 9)  //false
Sandy von
  • 57
  • 8
  • Place a breakpoint or a `debugger;` and see what value is assigned to `number` in each loop. – adiga May 04 '19 at 10:01
  • 1
    The behavior is not weird at all because in JavaScript all numbers are floating point numbers, hence `191 / 10` is `19.1` and not `19` as in Java. And `19.1 % 10 === 9.10000...1` – Andreas May 04 '19 at 10:04

3 Answers3

1

number /= 10 inside while(number > 0) will run many times, until precision fails. (eg, for 191: 191, then 19.1, then 1.91, then 0.191, ...) Probably better to split the number into an array of individual digits, then check if the digit you're looking for is included in that array:

function checkNumberIfContainsKey(number, key){
    return String(number).split('').includes(String(key))
}

console.log(
  checkNumberIfContainsKey(19, 9),   //true
  checkNumberIfContainsKey(191, 9),  //true
  checkNumberIfContainsKey(912, 9),  //true
  checkNumberIfContainsKey(185, 9)  //false
);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • That is why js shows weird behaviour? ... – Adelin May 04 '19 at 10:01
  • The `number%10 == key` test doesn't make sense, because `number` will often be a decimal number with OP's logic, not a whole number. (in which case the condition will always be false, except possibly on the first iteration) – CertainPerformance May 04 '19 at 10:04
  • oh my, bad I forgot javascript is loose in datatype it considers number as floating point number and shows such behavior. thank you for your help. – Sandy von May 04 '19 at 10:06
1

Use it like this

function checkNumberIfContainsKey(number, key){
  var a = !!number.toString().match(key)
  console.log(a)
  return a;
}

checkNumberIfContainsKey(19, 9)   //true
checkNumberIfContainsKey(191, 9)  //true
checkNumberIfContainsKey(912, 9)  //true
checkNumberIfContainsKey(185, 9)  //false
Shridhar Sharma
  • 2,337
  • 1
  • 9
  • 13
0

I was using java since long and just started javascript in java, all data belong to datatype and strictly defined so that works fine in java but in JS when I divided a number by other numbers if it results from decimal value then that variable automatically became floating point number so it never became less than 0.

the easiest solution ignoring decimal part by applying Math.trunc() function:

function checkNumberIfContainsKey(number, key){
    while(number > 0){
        if(number%10 == key){
            return true;
        }
        number = Math.trunc(number / 10);        
    }
    return false;
}
console.log(
  checkNumberIfContainsKey(19, 9),   //true
  checkNumberIfContainsKey(191, 9),  //true
  checkNumberIfContainsKey(912, 9),  //true
  checkNumberIfContainsKey(185, 9)  //false
);
Sandy von
  • 57
  • 8
  • _"automatically became floating point number"_ No, it hasn't changed its type because of the division. There are only floating point numbers in Javascript. – Andreas May 04 '19 at 13:11