Ad
Code
Diff
  • export function findTheLongestWord (sentence?: string): string{
      // You Can Code Below Here  
        if (sentence === undefined)
            throw new Error('gaboleh undefined')
        else if (sentence === null)
            throw new Error('gaboleh null')
        else if (typeof sentence !== 'string')
           throw new Error('harus string')
        else if (sentence.trim() === "")
            throw new Error('gaboleh kosong')
    
        const split = sentence.split(/\s+/);
        let terpanjang = "";
        let panjang = 0;
        let multi = false;
    
        for (const row of split) {
            if (row.length > panjang) {
                terpanjang = row;
                panjang = row.length;
                multi = false;
            } else if (row.length === panjang) {
                multi = true;
            }
        }
    
        if (multi) {
            return "no longest word found";
        } else {
            return `${terpanjang}: ${panjang} chars`;
        }
    }
    
    findTheLongestWord("I love Nagatech")
    findTheLongestWord("Programmer use Typescript")
    findTheLongestWord("Determine the Injustice")
    findTheLongestWord("Nagatech Sistem Integrator")
    findTheLongestWord("NGTC")
    findTheLongestWord("Kamu ska Typescript")
    • export function findTheLongestWord (sentence?: string): string{
    • // You Can Code Below Here
    • if (sentence === undefined) {
    • return 'gaboleh undefined'
    • }
    • if (sentence === null) {
    • return 'gaboleh null'
    • }
    • if (typeof sentence !== 'string') {
    • return 'harus string'
    • }
    • if (sentence.trim() === "") {
    • return 'gaboleh kosong'
    • }
    • if (sentence === undefined)
    • throw new Error('gaboleh undefined')
    • else if (sentence === null)
    • throw new Error('gaboleh null')
    • else if (typeof sentence !== 'string')
    • throw new Error('harus string')
    • else if (sentence.trim() === "")
    • throw new Error('gaboleh kosong')
    • const split = sentence.split(/\s+/);
    • let terpanjang = "";
    • let panjang = 0;
    • let multi = false;
    • for (const row of split) {
    • if (row.length > panjang) {
    • terpanjang = row;
    • panjang = row.length;
    • multi = false;
    • } else if (row.length === panjang) {
    • multi = true;
    • }
    • }
    • if (multi) {
    • return "no longest word found";
    • } else {
    • return `${terpanjang}: ${panjang} chars`;
    • }
    • }
    • findTheLongestWord()
    • findTheLongestWord("I love Nagatech")
    • findTheLongestWord("Programmer use Typescript")
    • findTheLongestWord("Determine the Injustice")
    • findTheLongestWord("Nagatech Sistem Integrator")
    • findTheLongestWord("NGTC")
    • findTheLongestWord("Kamu ska Typescript")