সমস্যা
আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা স্ট্রিং ক্লাসের প্রোটোটাইপ অবজেক্টে থাকবে। এই ফাংশনটি স্ট্রিংয়ের সকল অক্ষറকে বড় হাতের (uppercase) এ রূপান্তরিত করে নতুন স্ট্রিং ফেরত দেবে।
উদাহরণ
নিচে কোডটি দেওয়া হলো:
const str = 'This is a lowercase String';
String.prototype.customToUpperCase = function(){
const legend = 'abcdefghijklmnopqrstuvwxyz';
const UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
let res = '';
for(let i = 0; i < this.length; i++){
const el = this[i];
const index = legend.indexOf(el);
if(index !== -1){
res += UPPER[index];
}else{
res += el;
}
}
return res;
};
console.log(str.customToUpperCase());আউটপুট
কনসোল আউটপুট:
THIS IS A LOWERCASE STRING