কম্পিউটার

একটি টেক্সট ফাইল লোড করুন এবং ফাইলটিতে অক্ষরের সংখ্যা খুঁজুন - জাভাস্ক্রিপ্ট


ধরুন আমাদের একটি data.txt ফাইল আছে যা আমাদের NodeJS ফাইলের মতো একই ডিরেক্টরিতে থাকে। ধরুন সেই ফাইলের বিষয়বস্তু হল −

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s.

আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা এই বাহ্যিক টেক্সট ফাইলটি আমাদের js ফাইলে লোড করে এবং এই ফাইলের অক্ষরের সংখ্যা ফেরত দেয়৷

উদাহরণ

চলুন এই ফাংশনের জন্য কোড লিখি -

const fs = require('fs');
const requireFile = async () => {
   const data = fs.readFileSync('./data.txt', 'utf-8');
   const len = data.length;
   return len;
};
requireFile().then(res => console.log(res)).catch(err => {
   console.log('some error occured');
})

আউটপুট

কনসোলে আউটপুট:−

399

  1. জাভাস্ক্রিপ্টে ফাইল এবং ফাইলরিডার?

  2. জাভাস্ক্রিপ্ট ব্যবহার করে str2-এ উপস্থিত হওয়া str1-এর অক্ষরের সংখ্যা গণনা করুন এবং ফেরত দিন

  3. C++ এ n-এর নিকটতম এবং m দ্বারা বিভাজ্য সংখ্যাটি খুঁজুন

  4. পিএইচপি প্রোগ্রাম স্ট্রিং অক্ষর সংখ্যা খুঁজে বের করতে