Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript check if a number starts with another number

<script>
 
// javascript program for the above approach
   
 
// Function to check if B is a
// prefix of A or not
 
function checkprefix( A,  B)
{
   
    // Convert numbers into Strings
     
    var s1 = A.toString();
    var s2 = B.toString();
   
    // Find the lengths of Strings
    // s1 and s2
     
    var n1 = s1.length;
    var n2 = s2.length;
   
    // Base Case
    if (n1 < n2)
    {
        return false;
    }
   
    // Traverse the Strings s1 & s2
    for(var i = 0; i < n2; i++)
    {
   
        // If at any index characters
        // are unequals then return false
        if (s1[i] != s2[i])
        {
            return false;
        }
    }
   
    // Return true
    return true;
}
   
// Driver Code
 
       
    // Given numbers
    var A = 12345, B = 12;
   
    // Function call
    var result = checkprefix(A, B);
   
    // If B is a prefix of A, then
    // print "Yes"
    if (result)
    {
        document.write("Yes");
    }
    else
    {
        document.write("No");
    }
 
 
</script>
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #javascript #check #number #starts #number
ADD COMMENT
Topic
Name
2+5 =