Skip to content

Instantly share code, notes, and snippets.

@kumarldh
Created May 23, 2014 13:15
Show Gist options
  • Save kumarldh/26f3feaa56068939f50c to your computer and use it in GitHub Desktop.
Save kumarldh/26f3feaa56068939f50c to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Given a number count trailing zeros!</title>
<meta charset="UTF-8">
</head>
<body>
This is different from counting trailing zeroes from a given factorial.
<script>
function countTrailingZeros(input){
if(input === undefined || input < 10){
return 0;
}
var count = 0, multipleOfTen = 10, newvalue=0;
for(var i = 1; multipleOfTen <= input; i++) {
newvalue = parseInt(input/multipleOfTen);
if((newvalue * multipleOfTen ) === input){
multipleOfTen = multipleOfTen * 10;
count ++;
}else{
break;
}
}
return count;
}
console.log(countTrailingZeros(1000));
console.log(countTrailingZeros(1200));
console.log(countTrailingZeros(1230));
console.log(countTrailingZeros(1234));
console.log(countTrailingZeros(4));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment