Write a JavaScript program to create a function that gets the argument at index n. If n is negative, the nth argument from the end is returned

  • برمجة
  • برمجة جافاسكربت java script

Write a JavaScript program to create a function that gets the argument at index n. If n is negative, the nth argument from the end is returned.

الأجوبة

const nthArg = n => (...args) => args.slice(n)[0];
const third = nthArg(2);
third(1, 2, 3); // 3
third(1, 2); // undefined
const last = nthArg(-1);
console.log(last(1, 2, 3, 4, 5));
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...