মান লিজিয়ে, আমাদের একটি অ্যারে ফাংশন লিখতে হবে, যেনাম prependN(), যা একটি নম্বর n গ্রহণ করবে (n <= ফাংশনটি যেটির সাথে ব্যবহার করা হচ্ছে সেটির দৈর্ঘ্য) এবং এটি শেষ থেকে n সংখ্যক এলিমেন্ট নিয়ে অ্যারের সামনে রাখবে।
আমাদের এটি ইন-প্লেস করতে হবে এবং ফাংশনটি শুধু টাস্ক সফল হয়েছে কিনা সেটির উপর ভিত্তি করে একটি বুলিয়ান রিটার্ন করবে।
উদাহরণস্বরূপ:
// যদি ইনপুট অ্যারে হয়: const arr = ["blue", "red", "green", "orange", "yellow", "magenta", "cyan"]; // এবং নম্বর n হলো ৩, // তখন অ্যারেটি হবে এমন: const output = ["yellow", "magenta", "cyan", "blue", "red", "green", "orange"]; // এবং ফাংশনের রিটার্ন ভ্যালু হবে true
এখন, এই ফাংশনের কোড লিখি:
উদাহরণ
const arr = ["blue", "red", "green", "orange", "yellow", "magenta", "cyan"]; Array.prototype.reshuffle = function(num){ const { length: len } = this; if(num > len){ return false; }; const deleted = this.splice(len - num, num); this.unshift(...deleted); return true; }; console.log(arr.reshuffle(4)); console.log(arr);আউটপুট
কনসোলে আউটপুট হবে:
true [ 'orange', 'yellow', 'magenta', 'cyan', 'blue', 'red', 'green' ]