How to check if a number has zero in the center in TypeScript

1 Answer

0 votes
function hasZeroInCenter(num: number) {
    // Convert the number to a string
    const numStr: string = num.toString();
         
    if (numStr.length % 2 === 0) {
        return false; // Even length = no center digit
    }
         
    const centerIndex: number = Math.floor(numStr.length / 2);
         
    return numStr.charAt(centerIndex) === '0';
}
 
const num: number = 3720961;
 
if (hasZeroInCenter(num)) {
    console.log("yes");
} else {
    console.log("no");
}
 
  
  
/*
run:
   
"yes" 
   
*/

 



answered Sep 29, 2024 by avibootz

Related questions

1 answer 108 views
1 answer 130 views
1 answer 121 views
1 answer 122 views
1 answer 121 views
1 answer 109 views
1 answer 103 views
...