Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Array Manipulation in JavaScript, I am a little stuck any hint or suggestion is very much appreciated thank you.

Tasks:

1.Write a function called sliceElements that return the last 3 elements of any given array The starter code in the arraymanipulation.js file includes comments to help you along the way.

2.Write a function called spliceElements that would remove the last element of a given array and add 2 new elements at that position.

  1. Write a function called splitElements that would convert a given string into an array of words.

JS CODE:

/ Step 1: use slice to find elements in an array function sliceElements(givenArray) { //TODO: return last 3 elements of givenArray

var arrLenght = givenArray.length;

return givenArray.slice(arrLenght - 3, arrLenght); }

// Step 2: use splice to find elements in an array function spliceElements(givenArray, element1, element2) { //TODO: remove the last element of givenArray and add element1 and element2 at that position var arrLenght = givenArray.length; givenArray.splice(arrLenght - 1, 1, element1, element2); return givenArray; }

// Step 3: use splice to find elements in an array function splitElements(givenString) { //TODO: convert givenString into an array of words return givenString.trim().split(' '); }

//Uncomment these line to see results for Step 1 console.log(sliceElements([1, 2, 'MIT Certificate', 4, 5])); // should retrun ["MIT Certificate", 4, 5] console.log(sliceElements([1, 2, [3, 4], 'Javascript'])); // should retrun [2, [3, 4], "Javascript"]

//Uncomment these line to see results for Step 2 var arr = [1, 2, 'MIT Certificate', 4, 5]; console.log(spliceElements(arr, 'Javascript', 101)); // should return [1, 2, "MIT Certificate", 4, "Javascript", 101]

//Uncomment these line to see results for Step 3 var str = 'MIT Certificate loves Javascript '; console.log(splitElements(str)); // should return ["MIT", "Certificate", "loves", "Javascript"]

//don't change this line if (typeof module !== 'undefined') { module.exports = { sliceElements, spliceElements, splitElements }; }