Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (7k points)

I am working on LIME which is a subset of JavaScript. I am trying to use splice function to remove elements from an array but it is saying that LIME does not support splice function. Can anybody tell me a way to do this?

1 Answer

0 votes
by (13.1k points)

You can do like this:

function arraySplice(array, start, deleteCount)

 { var result = [];

 var removed = [];

 var argsLen = arguments.length;

 var arrLen = array.length;

 var i, k; 

// Follow spec more or less

 start = parseInt(start, 10);

 deleteCount = parseInt(deleteCount, 10);

 // Deal with negative start per spec // Don't assume support for Math.min/max if (start < 0)

 {

 start = arrLen + start; start = (start > 0)? start : 0;

 }

 else {

 start = (start < arrLen)? start : arrLen; 

// Deal with deleteCount per spec 

if (deleteCount < 0)

 deleteCount = 0;

if (deleteCount > (arrLen - start)) 

{

 deleteCount = arrLen - start;

 }

 // Copy members up to start

 for (i = 0; i < start; i++)

 {

 result[i] = array[i];

 }

 // Add new elements supplied as args

 for (i = 3; i < argsLen; i++)

 {

 result.push(arguments[i]);

 }

 // Copy removed items to removed array

 for (i = start; i < start + deleteCount; i++)

 {

 removed.push(array[i]);

 }

 // Add those after start + deleteCount

 for (i = start + (deleteCount || 0); i < arrLen; i++)

 {

 result.push(array[i]); 

}

 // Update original array 

array.length = 0;

 i = result.length;

 while (i--)

 {

 array[i] = result[i];

 }

 // Return array of removed elements

 return removed;

 }

Want to be a web developer? Check out the full stack developer course from Intellipaat.

Related questions

Browse Categories

...