mirror of
https://bitbucket.org/Mattrixwv/typescriptclasses.git
synced 2025-12-06 18:33:59 -05:00
Added functions to check for palindromes and convert nums to bin strings
This commit is contained in:
@@ -21,6 +21,7 @@ Copyright (C) 2021 Matthew Ellison
|
||||
*/
|
||||
|
||||
|
||||
import { SSL_OP_SSLEAY_080_CLIENT_DH_BUG } from "constants";
|
||||
import { InvalidResult } from "./InvalidResult";
|
||||
|
||||
|
||||
@@ -658,3 +659,37 @@ export function factorialBig(num: bigint): bigint{
|
||||
}
|
||||
return fact;
|
||||
}
|
||||
|
||||
//Returns true if the string passed in is a palindrome
|
||||
export function isPalindrome(str: string): boolean{
|
||||
let rev = str.split("").reverse().join("");
|
||||
if(str == rev){
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//Converts a number to its binary equivalent
|
||||
export function toBin(num: number): string{
|
||||
return (num >>> 0).toString(2);
|
||||
}
|
||||
export function toBinBig(num: bigint): string{
|
||||
let binNum = "";
|
||||
while(num > 0n){
|
||||
let rest = num % 2n;
|
||||
if(rest == 1n){
|
||||
binNum += "1";
|
||||
}
|
||||
else{
|
||||
binNum += "0";
|
||||
}
|
||||
num = (num - rest) / 2n;
|
||||
}
|
||||
binNum = binNum.split("").reverse().join("");
|
||||
if(binNum == ""){
|
||||
binNum = "0";
|
||||
}
|
||||
return binNum;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user