-
Code 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' } 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")
Test Cases Failed // See https://www.chaijs.com for how to use Chai. import { assert } from "chai"; import { findTheLongestWord } from "./solution"; // TODO Add your tests here describe("Longest Word Unit Test", function() { it("should throw an error if sent empty argument", function() { assert.throws(() => findTheLongestWord()) }); it("should return solution with string format `[word]: [length]`", function () { const result = findTheLongestWord("I love Nagatech") assert.strictEqual(result, "Nagatech: 8 chars") }) it("should return `no longest word found` if there's 2 or more word found", function () { const result = findTheLongestWord("Programmer use Typescript") assert.strictEqual(result, "no longest word found") }) }); describe("Longest Word Bonus Test", function () { it("Bonus #1", function (){ const result = findTheLongestWord("Determine the Injustice") assert.strictEqual(result, "no longest word found") }) it("Bonus #2", function() { const result = findTheLongestWord("Nagatech Sistem Integrator") assert.strictEqual(result, "Integrator: 10 chars") }) it("Bonus #3", function() { const result = findTheLongestWord("NGTC") assert.strictEqual(result, "NGTC: 4 chars") }) it("Bonus #4", function() { const result = findTheLongestWord("Kamu suka Typescript") assert.strictEqual(result, "Typescript: 10 chars") }) })
Output:
-
Code - export function findTheLongestWord (sentence?: string): string{
- // You Can Code Below Here
return ``}- 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'
- }
- 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")
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}