# The basic - Phần 2 **Published by:** [meteosrds](https://paragraph.com/@cryptovuive/) **Published on:** 2022-12-14 **URL:** https://paragraph.com/@cryptovuive/the-basic-ph-n-2 ## Content đáp án STRING MANIPULATIONfunction startsWithX(string) { let xstring = string.slice(0, 1); xstring = xstring.toLowerCase(); if (xstring === "x") { return true; } else { return false; } } module.exports = startsWithX; function startsWithX(string) { let xstring = string.slice(0, 1); xstring = xstring.toLowerCase(); if (xstring === "x") { return true; } else { return false; } } module.exports = startsWithX; function endsWithX(string) { let xstring = string.slice(-1); xstring = xstring.toLowerCase(); if (xstring !== "x") { return false; } else { return true; } } module.exports = endsWithX; 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; function findFirstX(string) { for (let i = 0; i < string.length; i++) { let xstring = string[i]; if (xstring === "x") { return i; } } } module.exports = findFirstX; 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 ARRAYSconst array = [1, 2, 3];// <-- create an array here! module.exports = array; function hasOne(array) { if (array.indexOf(1) === -1) { return false; } return true; } module.exports = hasOne; 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; 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; function addOne(array) { for (let i = 0; i < array.length; i++) { array[i] = array[i] + 1; } } module.exports = addOne; 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 OBJECTSconst order = { pizzas: 2, extraCheese: true, deliveryInstructions: "go to build" }; module.exports = order; function numberOfPizzas(order) { return order.pizzas; } module.exports = numberOfPizzas; function numberOfPizzas(orders) { let s = 0; for (let i = 0; i < orders.length; i++) { s = s + orders[i].pizzas; } return s; } module.exports = numberOfPizzas; const ORDER_TYPES = { PIZZA: 0, SALAD: 1, FRIES: 2 } module.exports = ORDER_TYPES; 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; function numberOfKeys(object) { let n = 0; for (let key in object) { n = n + 1; } return n; } module.exports = numberOfKeys; function removeSecret(object) { delete object.secret } module.exports = removeSecret; đáp án Practive Problems 2function shortestString(str1, str2) { if (str1.length < str2.length) { return str1; } else { return str2; } } module.exports = shortestString; 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; 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; 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; function reverse(string) { let retstr = ""; for (let i = 0; i < string.length; i++) { retstr = string[i] + retstr; } return retstr; } module.exports = reverse; 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; 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; 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; 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 OPERATORSfunction willEat(hasPizza, hasDonuts, hasCookies) { if (hasPizza || hasDonuts || hasCookies) { return true; } else { return false; } } module.exports = willEat; function double(x) { const def = x || "Undefined"; if ("Undefined" === def) { return 0; } else { return x * 2; } } module.exports = double; function canBreathe(isConnected, hasOxygen, aboveWater) { if (aboveWater) { return true; } if (isConnected && hasOxygen) { return true; } if (isConnected && aboveWater) { return true; } return false; } module.exports = canBreathe; function friendName(friend) { if (friend) { if (typeof (friend.name) != "undefined") { return friend.name; } } } module.exports = friendName; 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 EXCEPTIONSfunction throwError() { throw new Error("ee error !!."); } module.exports = throwError; function catchError(fn) { try { fn(); } catch (ex) { console.log(ex); } } module.exports = catchError; function catchError(fn) { try { fn(); } catch (ex) { return ex; } return false; } module.exports = catchError; function startError() { let abc; abc.prop; } module.exports = startError; đáp án TYPE CONVERSIONfunction toNumber(string) { let n = Number(string); if (isNaN(n)) { return 0; } else { return n; } } module.exports = toNumber; function combineToString(a, b) { return String(a) + String(b); } module.exports = combineToString; function isTruthy(a) { return Boolean(a); } module.exports = isTruthy; function looseEquals(a, b) { return a == b; } module.exports = looseEquals; function toJSON(obj) { return JSON.stringify(obj); } module.exports = toJSON; const personJSON = ` { "name": "jami", "age": 36, "isReal": false } `; module.exports = personJSON; ## Publication Information - [meteosrds](https://paragraph.com/@cryptovuive/): Publication homepage - [All Posts](https://paragraph.com/@cryptovuive/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@cryptovuive): Subscribe to updates - [Twitter](https://twitter.com/meteosrds): Follow on Twitter