Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,950 questions

51,892 answers

573 users

How to remove characters from a numeric string such that the string becomes divisible by 8 in TypeScript

1 Answer

0 votes
function checkIfNumExist(num: string, s: string) {
    let index: number = 0;
    
    for (let ch of s) {
        if (num[index] === ch) {
            index++;
            if (num.length === index) {
                break;
            }
        }
    }
    
    return index === num.length ? 1 : 0;
}

function convertStringToBecomesDivisibleBy8(s: string) {
    for (let i: number = 8; i < 1e3; i += 8) {
        let num: string = i.toString();
        if (checkIfNumExist(num, s) === 1) {
            return num;
        }
    }
    
    return "-1";
}

let s1: string = "127892";
let result: string = convertStringToBecomesDivisibleBy8(s1);
console.log(result);

let s2: string = "1201674";
result = convertStringToBecomesDivisibleBy8(s2);
console.log(result);

let s3: string = "1209574";
result = convertStringToBecomesDivisibleBy8(s3);
console.log(result);

let s4: string = "190395473";
result = convertStringToBecomesDivisibleBy8(s4);
console.log(result);

let s5: string = "1309577";
result = convertStringToBecomesDivisibleBy8(s5);
if (result !== "-1") {
    s5 = result;
    console.log(s5);
} else {
    console.log("No numeric characters combination divisible by 8");
}




/*
run:

"8" 
"16" 
"24" 
"104" 
"No numeric characters combination divisible by 8" 

*/

 



answered Mar 19, 2024 by avibootz
...