Cover photo

The basic - Phần 2

đáp án STRING MANIPULATION

post image
post image
function startsWithX(string) {
    let xstring = string.slice(0, 1);

    xstring = xstring.toLowerCase();
    if (xstring === "x") {
        return true;
    }
    else {
        return false;
    }
}

module.exports = startsWithX;
post image
function startsWithX(string) {
    let xstring = string.slice(0, 1);

    xstring = xstring.toLowerCase();
    if (xstring === "x") {
        return true;
    }
    else {
        return false;
    }
}

module.exports = startsWithX;
post image
function endsWithX(string) {
    let xstring = string.slice(-1);

    xstring = xstring.toLowerCase();
    if (xstring !== "x") {
        return false;
    }
    else {
        return true;
    }
}

module.exports = endsWithX;
post image
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;
post image
function findFirstX(string) {
    for (let i = 0; i < string.length; i++) {
        let xstring = string[i];

        if (xstring === "x") {
            return i;
        }
    }
}

module.exports = findFirstX;
post image
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

post image
post image
const array = [1, 2, 3];// <-- create an array here!

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

    return true;
}

module.exports = hasOne;
post image
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;
post image
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;
post image
function addOne(array) {
    for (let i = 0; i < array.length; i++) {
        array[i] = array[i] + 1;
    }
}

module.exports = addOne;
post image
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

post image
post image
const order = {
    pizzas: 2,
    extraCheese: true,
    deliveryInstructions: "go to build"
};

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

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

    return s;
}

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

module.exports = ORDER_TYPES;
post image
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;
post image
function numberOfKeys(object) {
    let n = 0;
    for (let key in object) {
        n = n + 1;
    }

    return n;
}

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

module.exports = removeSecret;

đáp án Practive Problems 2

post image
post image
function shortestString(str1, str2) {
    if (str1.length < str2.length) {
        return str1;
    } else {
        return str2;
    }
}

module.exports = shortestString;
post image
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;
post image
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;
post image
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;
post image
function reverse(string) {
    let retstr = "";
    for (let i = 0; i < string.length; i++) {
        retstr = string[i] + retstr;
    }

    return retstr;
}

module.exports = reverse;
post image
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;
post image
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;
post image
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;
post image
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

post image
post image
function willEat(hasPizza, hasDonuts, hasCookies) {
    if (hasPizza || hasDonuts || hasCookies) {
        return true;
    } else {
        return false;
    }
}

module.exports = willEat;
post image
function double(x) {
    const def = x || "Undefined";

    if ("Undefined" === def) {
        return 0;
    } else {
        return x * 2;
    }
}

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

    return false;
}

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

module.exports = friendName;
post image
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

post image
post image
function throwError() {
    throw new Error("ee error !!.");
}

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

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

    return false;
}

module.exports = catchError;
post image
function startError() {
    let abc;

    abc.prop;
}

module.exports = startError;

đáp án TYPE CONVERSION

post image
post image
function toNumber(string) {
    let n = Number(string);
    if (isNaN(n)) {
        return 0;
    } else {
        return n;
    }
}

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

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

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

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

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

module.exports = personJSON;