custom
Let's create custom JavaScript functions that mimic the behavior of some of these built-in functions. This can be a useful exercise to understand how these functions work under the hood.
1. Custom String Functions
charAt(index)
function customCharAt(str, index) {
return str[index] || '';
}
console.log(customCharAt("Hello, World!", 0)); // "H"
concat(string2, string3, ..., stringX)
function customConcat(...strings) {
return strings.join('');
}
console.log(customConcat("Hello", ", ", "World", "!")); // "Hello, World!"
includes(searchString, position)
function customIncludes(str, searchString, position = 0) {
return str.indexOf(searchString, position) !== -1;
}
console.log(customIncludes("Hello, World!", "World")); // true
indexOf(searchValue, fromIndex)
function customIndexOf(str, searchValue, fromIndex = 0) {
for (let i = fromIndex; i < str.length; i++) {
if (str.substring(i, i + searchValue.length) === searchValue) {
return i;
}
}
return -1;
}
console.log(customIndexOf("Hello, World!", "World")); // 7
replace(searchValue, newValue)
function customReplace(str, searchValue, newValue) {
return str.split(searchValue).join(newValue);
}
console.log(customReplace("Hello, World!", "World", "Everyone")); // "Hello, Everyone!"
slice(beginIndex, endIndex)
function customSlice(str, beginIndex, endIndex = str.length) {
let result = '';
for (let i = beginIndex; i < endIndex; i++) {
result += str[i];
}
return result;
}
console.log(customSlice("Hello, World!", 7, 12)); // "World"
split(separator, limit)
function customSplit(str, separator, limit) {
let result = [];
let temp = '';
for (let i = 0; i < str.length; i++) {
if (str[i] === separator) {
result.push(temp);
temp = '';
if (result.length === limit) {
return result;
}
} else {
temp += str[i];
}
}
result.push(temp);
return result;
}
console.log(customSplit("Hello, World!", ", ")); // ["Hello", "World!"]
toLowerCase()
function customToLowerCase(str) {
let result = '';
for (let i = 0; i < str.length; i++) {
let charCode = str.charCodeAt(i);
if (charCode >= 65 && charCode <= 90) {
result += String.fromCharCode(charCode + 32);
} else {
result += str[i];
}
}
return result;
}
console.log(customToLowerCase("Hello, World!")); // "hello, world!"
toUpperCase()
function customToUpperCase(str) {
let result = '';
for (let i = 0; i < str.length; i++) {
let charCode = str.charCodeAt(i);
if (charCode >= 97 && charCode <= 122) {
result += String.fromCharCode(charCode - 32);
} else {
result += str[i];
}
}
return result;
}
console.log(customToUpperCase("Hello, World!")); // "HELLO, WORLD!"
2. Custom Array Functions
concat(array2, array3, ..., arrayX)
function customArrayConcat(...arrays) {
let result = [];
arrays.forEach(array => {
array.forEach(item => result.push(item));
});
return result;
}
console.log(customArrayConcat([1, 2, 3], [4, 5, 6])); // [1, 2, 3, 4, 5, 6]
every(callback)
function customEvery(array, callback) {
for (let i = 0; i < array.length; i++) {
if (!callback(array[i], i, array)) {
return false;
}
}
return true;
}
console.log(customEvery([1, 2, 3, 4, 5], num => num < 6)); // true
3. Custom Math Functions
random()
function customRandom() {
return Math.random();
}
console.log(customRandom()); // Random number between 0 and 1
round(x)
function customRound(x) {
return (x % 1 >= 0.5) ? Math.ceil(x) : Math.floor(x);
}
console.log(customRound(4.5)); // 5
sqrt(x)
function customSqrt(x) {
if (x < 0) return NaN;
let approx = x / 2;
let betterApprox;
while (true) {
betterApprox = (approx + x / approx) / 2;
if (Math.abs(betterApprox - approx) < 0.000001) break;
approx = betterApprox;
}
return betterApprox;
}
console.log(customSqrt(16)); // 4
min(x, y, ..., z)
function customMin(...numbers) {
let minNum = numbers[0];
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] < minNum) {
minNum = numbers[i];
}
}
return minNum;
}
console.log(customMin(1, 2, 3)); // 1
4. Custom Date Functions
getDate()
function customGetDate(date) {
return date.getDate();
}
let date1 = new Date();
console.log(customGetDate(date1)); // Current day of the month
setDate(day)
function customSetDate(date, day) {
date.setDate(day);
return date;
}
let date9 = new Date();
console.log(customSetDate(date9, 15)); // Date with day set to 15
setFullYear(year, month, day)
function customSetFullYear(date, year, month, day) {
date.setFullYear(year, month, day);
return date;
}
let date10 = new Date();
console.log(customSetFullYear(date10, 2024, 5, 15)); // Date set to June 15, 2024
setHours(hour, min, sec, millisec)
function customSetHours(date, hour, min, sec, millisec) {
date.setHours(hour, min, sec, millisec);
return date;
}
let date11 = new Date();
console.log(customSetHours(date11, 15, 30, 0, 0)); // Date with time set to 15:30:00.000
5. Custom Number Functions
isInteger(value)
function customIsInteger(value) {
return typeof value === 'number' && isFinite(value) && Math.floor(value) === value;
}
console.log(customIsInteger(10)); // true
console.log(customIsInteger(10.5)); // false
isNaN(value)
function customIsNaN(value) {
return value !== value;
}
console.log(customIsNaN(NaN)); // true
console.log(customIsNaN(10)); // false
These custom functions are simplified versions of the built-in JavaScript functions, demonstrating the core logic behind them. They may not cover all edge cases and nuances of the built-in versions but should provide a good understanding of how they work.