# The basic - Phần 2

By [meteosrds](https://paragraph.com/@cryptovuive) · 2022-12-14

---

**đáp án STRING MANIPULATION**

![](https://storage.googleapis.com/papyrus_images/f39c9da09e1a7ff704de0f1d0bee90d46b82cdeb536e8d282405505de5be22a6.png)

![](https://storage.googleapis.com/papyrus_images/622223249c431bce1b36b5ba0171d03376b0ed699b57560771e5ad56a4f8a742.png)

    function startsWithX(string) {
        let xstring = string.slice(0, 1);
    
        xstring = xstring.toLowerCase();
        if (xstring === "x") {
            return true;
        }
        else {
            return false;
        }
    }
    
    module.exports = startsWithX;
    

![](https://storage.googleapis.com/papyrus_images/49c8da5232f7ff29063bb20bf6dc8cb8d358c30a8baaaa8a0bff4d955b00acf5.png)

    function startsWithX(string) {
        let xstring = string.slice(0, 1);
    
        xstring = xstring.toLowerCase();
        if (xstring === "x") {
            return true;
        }
        else {
            return false;
        }
    }
    
    module.exports = startsWithX;
    

![](https://storage.googleapis.com/papyrus_images/6fe41cc4bd2d04240a1933d48ace080c9377d66d4cf8442168cb4bf06512509a.png)

    function endsWithX(string) {
        let xstring = string.slice(-1);
    
        xstring = xstring.toLowerCase();
        if (xstring !== "x") {
            return false;
        }
        else {
            return true;
        }
    }
    
    module.exports = endsWithX;
    

![](https://storage.googleapis.com/papyrus_images/0a3aabd1527a07ee90ef009fa8fccfe6adaf7780663fa83134d68f79d9b5ce8d.png)

    function isAllX(string) {
        for (let i = 0; i < string.length; i++) {
            let xstring = string[i];
            xstring = xstring.toUpperCase();
            if (xstring !== "X") {
                return false;
            }
        }
    
        return true;
    }
    
    module.exports = isAllX;
    

![](https://storage.googleapis.com/papyrus_images/aae3f54b7df7f89eca7947f208f26d3af19aec3d4c8875a30f4274234c4569fe.png)

    function findFirstX(string) {
        for (let i = 0; i < string.length; i++) {
            let xstring = string[i];
    
            if (xstring === "x") {
                return i;
            }
        }
    }
    
    module.exports = findFirstX;
    

![](https://storage.googleapis.com/papyrus_images/bbe28827b5fa0d3353717632e31061829faa7bead63dec1201842e65c1fc5674.png)

    function splitAtX(string) {
        for (let i = 0; i < string.length; i++) {
            let xstring = string[i];
    
            if (xstring === "x") {
                xstring1 = string.slice(i + 1, string.length);
                xstring2 = string.slice(0, i);
                if (xstring1.length < xstring2.length) {
                    return xstring2;
                }
                else {
                    return xstring1;
                }
            }
        }
    }
    
    module.exports = splitAtX;
    

đáp án ARRAYS
-------------

![](https://storage.googleapis.com/papyrus_images/e7313a88199bcd14213ad950c3a94f6ca587c53a263fb8d0621654cf7833ebfd.png)

![](https://storage.googleapis.com/papyrus_images/09ea4d1720031754959b840bf79baddc3b2aca15f62837036f283d254168059f.png)

    const array = [1, 2, 3];// <-- create an array here!
    
    module.exports = array;
    

![](https://storage.googleapis.com/papyrus_images/5baeacc273ab92886a8fe9d29fc5d945ff04612534fb135324554411e855a595.png)

    function hasOne(array) {
        if (array.indexOf(1) === -1) {
            return false;
        }
    
        return true;
    }
    
    module.exports = hasOne;
    

![](https://storage.googleapis.com/papyrus_images/b9d80e810080956e620305b63c76e50a89c168f2a53318aa9d23c8bd8b21e12c.png)

    function sumEven(array) {
        let s = 0;
        for (let i = 0; i < array.length; i++) {
            if (array[i] % 2 === 0) {
                s = s + array[i];
            }
        }
    
        return s;
    }
    
    module.exports = sumEven;
    

![](https://storage.googleapis.com/papyrus_images/5b65fdc469fde4393cca89efb4aa19c3f551897ff2d6677d4b42027bfbe81d01.png)

    function unique(array) {
        let r = [];
        for (let i = 0; i < array.length; i++) {
            if (r.indexOf(array[i]) === -1) {
                r.push(array[i]);
            }
        }
        return r;
    }
    
    module.exports = unique;
    

![](https://storage.googleapis.com/papyrus_images/47e4da913178efaacba35adf46153619ac16f9967aac4950d3016ac8f3b3c14d.png)

    function addOne(array) {
        for (let i = 0; i < array.length; i++) {
            array[i] = array[i] + 1;
        }
    }
    
    module.exports = addOne;
    

![](https://storage.googleapis.com/papyrus_images/e155b0e76f66279f7982c60d2e3a3aad323fc984a3975256c2591e36b7920e85.png)

    function removeOccurrences(array, num) {
        for (let i = array.indexOf(num); i !== -1; i = array.indexOf(num)) {
            array.splice(i, 1);
        }
    }
    
    module.exports = removeOccurrences;
    

đáp án OBJECTS
--------------

![](https://storage.googleapis.com/papyrus_images/79c66d2728468b7446c2ddfc9c16f43a82e2ab55dc35b36ffcd9d243d585d47e.png)

![](https://storage.googleapis.com/papyrus_images/e000b55695b4636180140d1d2d8ac96234c1e8a79d58412cf450ce5439a05af6.png)

    const order = {
        pizzas: 2,
        extraCheese: true,
        deliveryInstructions: "go to build"
    };
    
    module.exports = order;
    

![](https://storage.googleapis.com/papyrus_images/ea0a23e2309944e0699645e94d24c9a39401d0eef7e948a9248f9e0d71c59686.png)

    function numberOfPizzas(order) {
        return order.pizzas;
    }
    
    module.exports = numberOfPizzas;
    

![](https://storage.googleapis.com/papyrus_images/6c50eb2b1dd3dddfaa081f16b4fd1a230afacf155396e3b5be60f97d4b5c703f.png)

    function numberOfPizzas(orders) {
        let s = 0;
        for (let i = 0; i < orders.length; i++) {
            s = s + orders[i].pizzas;
        }
    
        return s;
    }
    
    module.exports = numberOfPizzas;
    

![](https://storage.googleapis.com/papyrus_images/d2cc0018fd3b6cdb4a7cd1662acd58e5d21a4e0d111c728d32bcc038187cdd73.png)

    const ORDER_TYPES = {
        PIZZA: 0,
        SALAD: 1,
        FRIES: 2
    }
    
    module.exports = ORDER_TYPES;
    

![](https://storage.googleapis.com/papyrus_images/1e4e1bd855cdd993284ba43c96d716c2a714a6862329b5a0ab7e90bcb41e9cec.png)

    const ORDER_TYPES = require('./orderTypes');
    
    function numberOfPizzas(orders) {
        let s = 0;
    
        for (let i = 0; i < orders.length; i++) {
            if (orders[i].type === ORDER_TYPES.PIZZA) {
                s = s + orders[i].pizzas;
            }
        }
        return s;
    }
    
    module.exports = numberOfPizzas;
    

![](https://storage.googleapis.com/papyrus_images/5536fd028fe232ed0b236e0e73a952db96e64a71b10c28f74ae888c3e095231e.png)

    function numberOfKeys(object) {
        let n = 0;
        for (let key in object) {
            n = n + 1;
        }
    
        return n;
    }
    
    module.exports = numberOfKeys;
    

![](https://storage.googleapis.com/papyrus_images/54526e00aeb846818b7a474368446b9516962b00b9e6f5ad60fe00f775881408.png)

    function removeSecret(object) {
        delete object.secret
    }
    
    module.exports = removeSecret;
    

đáp án Practive Problems 2
--------------------------

![](https://storage.googleapis.com/papyrus_images/77b95b9f574308dc5faa761a40aeaa19b1fbeed4c4bdd3af0c70d2fd678b2e5c.png)

![](https://storage.googleapis.com/papyrus_images/c66db07e50c211e4d4fac1f34542672f8b645a72371f0efe0ea18e0f865b54ea.png)

    function shortestString(str1, str2) {
        if (str1.length < str2.length) {
            return str1;
        } else {
            return str2;
        }
    }
    
    module.exports = shortestString;
    

![](https://storage.googleapis.com/papyrus_images/3305284da8d531af96de0ff9ba245fe59dc16c4c3d38891074e0fedfbe5c3f4e.png)

    function halfValue(numbers) {
        let retarr = [];
    
        for (let i = 0; i < numbers.length; i++) {
            if (0 === numbers[i] % 2) {
                retarr.push(numbers[i] / 2);
            } else {
                let tmp = Math.floor(numbers[i] / 2) + 1;
                retarr.push(tmp);
            }
        }
    
        return retarr;
    }
    
    module.exports = halfValue;
    

![](https://storage.googleapis.com/papyrus_images/d1eb564857f20567031d8acd67fd474f00170a0dcee0b4c983cf9c35c75c29ca.png)

    function countC(str) {
        let c = 0;
        let cstr = str.toLowerCase();
        let i = cstr.indexOf("c");
    
        while (-1 !== i) {
            c = c + 1;
            i = cstr.indexOf("c", i + 1);
        }
    
        return c;
    }
    
    module.exports = countC;
    

![](https://storage.googleapis.com/papyrus_images/33edbff5c6a645498820cce2e46444be7f9d736955756eb04db3f6c1992d6ffc.png)

    function countVowels(str) {
        let vowels = "aeiou";
        let c = 0;
        let vowelstr = str.toLowerCase();
    
        for (let i = 0; i < vowelstr.length; i++) {
            if (-1 !== vowels.indexOf(vowelstr[i])) {
                c = c + 1;
            }
        }
    
        return c;
    }
    
    module.exports = countVowels;
    

![](https://storage.googleapis.com/papyrus_images/1ab28df7caf93ef6e68446c45011240af8da241d857f560008ca54c12c89a31c.png)

    function reverse(string) {
        let retstr = "";
        for (let i = 0; i < string.length; i++) {
            retstr = string[i] + retstr;
        }
    
        return retstr;
    }
    
    module.exports = reverse;
    

![](https://storage.googleapis.com/papyrus_images/b7700771d37a21db7914f833a7219f7fa3009a915da9c7e43252d41dd8073849.png)

    function isPalindrome(string) {
        let i = 0;
        let endi = string.length - 1;
        while (endi >= i) {
            if (string[i] === string[endi]) {
                i = i + 1;
                endi = endi - 1;
            } else {
                return false;
            }
        }
    
        return true;
    }
    
    module.exports = isPalindrome;
    

![](https://storage.googleapis.com/papyrus_images/244b8921f2562b0d6dc511d53f17b93175b5566f1e42c96933ca26ef65f26ce3.png)

    function sumTogether(arr1, arr2) {
        let a = [];
    
        for (let i = 0; i < arr1.length; i++) {
            a[i] = arr1[i] + arr2[i];
        }
    
        return a;
    }
    
    module.exports = sumTogether;
    

![](https://storage.googleapis.com/papyrus_images/689448d098d232bd7474f459f50f08db45c47942a17b53aee08a13efc4e67741.png)

    function countElements(elements) {
        let countEle = {};
    
        for (let i = 0; i < elements.length; i++) {
            if (countEle.hasOwnProperty(elements[i])) {
                countEle[elements[i]] = countEle[elements[i]] + 1;
            } else {
                countEle[elements[i]] = 1;
            }
        }
    
        return countEle;
    }
    
    module.exports = countElements;
    

![](https://storage.googleapis.com/papyrus_images/b015aa8604bc1acda861bb78c4f559aabd637af97413c0f657f3372b1d92bc63.png)

    function playerHandScore(hand) {
        const jqkScore = { "J": 2, "Q": 3, "K": 4 };
        let s = 0;
    
        for (let i = 0; i < hand.length; i++) {
            s = s + jqkScore[hand[i]];
        }
    
        return s;
    }
    
    module.exports = playerHandScore;
    

đáp án LOGICAL OPERATORS
------------------------

![](https://storage.googleapis.com/papyrus_images/b5f961eb9cb1bf0ef0e0191fc2e2e5774abb5451ae87ec233d3df42bceb76f7f.png)

![](https://storage.googleapis.com/papyrus_images/5878875e7549c54d7678c54ba7442c0eeb6d78fa04b82c76624e49d7c8b49c54.png)

    function willEat(hasPizza, hasDonuts, hasCookies) {
        if (hasPizza || hasDonuts || hasCookies) {
            return true;
        } else {
            return false;
        }
    }
    
    module.exports = willEat;
    

![](https://storage.googleapis.com/papyrus_images/5a45454c2957fcc2eb716770504e21673ba22c96b642a7a556ea853102c11508.png)

    function double(x) {
        const def = x || "Undefined";
    
        if ("Undefined" === def) {
            return 0;
        } else {
            return x * 2;
        }
    }
    
    module.exports = double;
    

![](https://storage.googleapis.com/papyrus_images/a40b839337554ce26865867258b8903345f30538abf9d93069e37a55f40ea5e7.png)

    function canBreathe(isConnected, hasOxygen, aboveWater) {
        if (aboveWater) {
            return true;
        }
        if (isConnected && hasOxygen) {
            return true;
        }
        if (isConnected && aboveWater) {
            return true;
        }
    
        return false;
    }
    
    module.exports = canBreathe;
    

![](https://storage.googleapis.com/papyrus_images/5479f120969020ce3f66c19a218a82131f321d8b7060086e58c10d028c20abec.png)

    function friendName(friend) {
        if (friend) {
            if (typeof (friend.name) != "undefined") {
                return friend.name;
            }
        }
    }
    
    module.exports = friendName;
    

![](https://storage.googleapis.com/papyrus_images/ea26da5a6c9e01315547d290250be3eaf89f9bd63559d7c003ce6cf504d5512b.png)

    function carCrossing(aCrossing, bCrossing) {
        if (aCrossing) {
            if (bCrossing) {
                return false;
            } else {
                return true;
            }
        } else {
            if (bCrossing) {
                return true;
            } else {
                return false;
            }
        }
    }
    
    module.exports = carCrossing;
    

đáp án EXCEPTIONS
-----------------

![](https://storage.googleapis.com/papyrus_images/c743be24b95a2f26c46d31895eb6ea188eb897f6ca023ea17fe9d5c2cebd6995.png)

![](https://storage.googleapis.com/papyrus_images/70ebe9b988a3127fb0aecef6580a5985cfc9c0b404de06c567632999b3cd5536.png)

    function throwError() {
        throw new Error("ee error !!.");
    }
    
    module.exports = throwError;
    

![](https://storage.googleapis.com/papyrus_images/62aa876ace10e3d27b9dc5826239317edd54098604cb9ad44cd923e2e92f2ea7.png)

    function catchError(fn) {
        try {
            fn();
        } catch (ex) {
            console.log(ex);
        }
    }
    
    module.exports = catchError;
    

![](https://storage.googleapis.com/papyrus_images/42be023d4e42a579c27df237d9ff517c9e14b4b872fe756d9a91a81fa1f3b5ad.png)

    function catchError(fn) {
        try {
            fn();
        } catch (ex) {
            return ex;
        }
    
        return false;
    }
    
    module.exports = catchError;
    

![](https://storage.googleapis.com/papyrus_images/71884d0f71d01608026c9f6c50ccfdec5b55d50a280113ccfed0aeb98999647f.png)

    function startError() {
        let abc;
    
        abc.prop;
    }
    
    module.exports = startError;
    

đáp án TYPE CONVERSION
----------------------

![](https://storage.googleapis.com/papyrus_images/b0c1dad73701c8726e3375a8bda320028a2811a31a045ff19dec084c51efedae.png)

![](https://storage.googleapis.com/papyrus_images/47ddb8a46723160fe75e35b899b6282d0bffa57837c3528226926c2b4b3e20f8.png)

    function toNumber(string) {
        let n = Number(string);
        if (isNaN(n)) {
            return 0;
        } else {
            return n;
        }
    }
    
    module.exports = toNumber;
    

![](https://storage.googleapis.com/papyrus_images/044b18c6397d606161663bce344a47ada3452e081afe5c1c77b0f25ce4f8dd6a.png)

    function combineToString(a, b) {
        return String(a) + String(b);
    }
    
    module.exports = combineToString;
    

![](https://storage.googleapis.com/papyrus_images/dc76c5126209f663ef1aaed9f2c433b1ba994a15e62e87ddc20b0006691a8449.png)

    function isTruthy(a) {
        return Boolean(a);
    }
    
    module.exports = isTruthy;
    

![](https://storage.googleapis.com/papyrus_images/4cdc5e09a6edbc9f564c9b3d1a2bcd2d61aa8064ca2aa61b78ccf4494d3112a1.png)

    function looseEquals(a, b) {
        return a == b;
    }
    
    module.exports = looseEquals;
    

![](https://storage.googleapis.com/papyrus_images/0df39018a5bf95bfb7f27577c7226f76810e33ced4d3fcda5b12703ca0f0456d.png)

    function toJSON(obj) {
        return JSON.stringify(obj);
    }
    
    module.exports = toJSON;
    

![](https://storage.googleapis.com/papyrus_images/6b9bbaaf661fdb2c5f50b821710487028c290a5d78901a2bb2c8b272ef02c269.png)

    const personJSON = `
        {
            "name": "jami",
            "age": 36,
            "isReal": false
        } 
    `;
    
    module.exports = personJSON;

---

*Originally published on [meteosrds](https://paragraph.com/@cryptovuive/the-basic-ph-n-2)*
