Arrays are list-like objects whose prototype has methods to perform traversal and mutations. Neither the length of JavaScript array nor their elements are fixed. Array are widely used in web applications, below will explain most widely used prototype method Slice. Most of the tricky questions or algorithims asked will make use of Slice methods. They plays important roles in Functional Programming.
Important things about Slice Method:
Caution:
The slice method is not to be confused with the splice method, which modifies an array in place.
Syntax Slice
arr.slice(begin[, end]])For more details about how it works refer MDN Documentations
Interview Tips
function getShiftedString(s, leftShifts, rightShifts) {
const arr = [...s]
const netLeftShifts = (leftShifts - rightShifts) % arr.length;
return [...arr.slice(netLeftShifts), ...arr.slice(0, netLeftShifts)]
.join('');
}
console.log(getShiftedString("UnitedWeStand", 2, 1));