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 :: shrinkStringByRepeatFactor 
Javascript :: how to check if a div tag contains child 
Javascript :: React Gotchas 
Javascript :: adding javascript object within array in the last position 
Javascript :: javascript count number of lines of a text 
Javascript :: Solution-4-B--solution options for reverse bits algorithm js 
Javascript :: c# to javascript object 
Javascript :: 2d array js 
Javascript :: empty an array in javascript 
Javascript :: anonymous function js 
Javascript :: array max in javascript 
Javascript :: button click event 
Javascript :: javascript oop 
Javascript :: .reverse javascript string 
Javascript :: jsx classname 
Javascript :: regex javscript 
Javascript :: generator function in javascript 
Javascript :: graph data structure in js 
Javascript :: react native file pdf to base64 
Javascript :: exit react native app 
Javascript :: java script layout engine error 
Javascript :: javascript rest parameter 
Javascript :: freecodecamp javascript basic step quoting string 
Javascript :: react destructuring with rename 
Javascript :: lookup in other document in array 
Javascript :: How to Update the Props of a Rendered Component in vue Testing Library 
Javascript :: Cntrlsss:$.Control-Ai 
Javascript :: phaser animation on stop event 
Javascript :: Six escape sequences are valid in JavaScript 
Javascript :: show json preformatted 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =