Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript check if a number starts with another number

// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
 
// Function to check if B is a
// prefix of A or not
bool checkprefix(int A, int B)
{
 
    // Convert numbers into strings
    string s1 = to_string(A);
    string s2 = to_string(B);
 
    // Find the lengths of strings
    // s1 and s2
    int n1 = s1.length();
    int n2 = s2.length();
 
    // Base Case
    if (n1 < n2) {
        return false;
    }
 
    // Traverse the strings s1 & s2
    for (int 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
int main()
{
    // Given numbers
    int A = 12345, B = 12;
 
    // Function Call
    bool result = checkprefix(A, B);
 
    // If B is a prefix of A, then
    // print "Yes"
    if (result) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}
Comment

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>
Comment

Check if a number starts with another number or not js

let theNumber = 132435;
//1. Convert the number to string
theNumber = theNumber.toString(); // '132435'
//2. Save the result in boolean
let startWithMyNumber = theNumber.startsWith('13'); //true
Comment

PREVIOUS NEXT
Code Example
Javascript :: Declaring A Internal Method Of A Class 
Javascript :: A Node Module For ReactJS 
Javascript :: select next occurrence visual studio 
Javascript :: reduxjs toolkit createaction 
Javascript :: photoshop Change image size JavaScript 
Javascript :: Javascript Area When Base and Height is Known 
Javascript :: confirming the end javascript 
Javascript :: Download A File With Link Using ExpressJS 
Javascript :: lowercase vs lower locale 
Javascript :: checkbox null value javascript 
Javascript :: convert .js to .ts 
Javascript :: general hardhat config js file code 
Javascript :: Looping through array, fetching tweets and returning new reversed array javascript react 
Javascript :: javascript remove the second to last character of a string 
Javascript :: js beutify node.js 
Javascript :: next.js api typescript 
Javascript :: how to create existing nodes in godot 
Javascript :: name of javascript virtual machine for apple 
Javascript :: how to change array elements position in array in javascript 
Javascript :: Solution-2--solution options for reverse bits algorithm js 
Javascript :: jquery loop through model list 
Javascript :: cookies javascript 
Javascript :: how to define class in javascript 
Javascript :: javascript alarm 
Javascript :: javascript link to page 
Javascript :: req.body showing undefined 
Javascript :: nlhoman json load from file 
Javascript :: API key header for appsync graphql request 
Javascript :: javascript prototype chaining 
Javascript :: convert dom object to string 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =