Skip to content

Instantly share code, notes, and snippets.

@McLarenCollege
Last active March 17, 2022 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McLarenCollege/54d537846ad7f20066d51cc47b3fd843 to your computer and use it in GitHub Desktop.
Save McLarenCollege/54d537846ad7f20066d51cc47b3fd843 to your computer and use it in GitHub Desktop.
Exercise : Change Last Element to X

Part1

Create a function that creates a copy of the given array and changes the last element to 'X'.

Note: The change made should not affect the original array.

function changeLastElementToX(arr){
// write your code here
}
let orig = [ 2, 5, 6, 4, 8]
let copy = changeLastElementToX(orig);
console.log(copy);
console.log(orig);

Part2

Complete the function below to change the value of bottom right corner element to 'X'.

Note: The change made should not affect the original array.

function bottomRightElementToX(arr) {
    //write your code here.
}

let orig1 = [
    [5, 2, 4],
    [4, 1, 7],
    [7, 12, 2]
]
let copy1 = bottomRightElementToX(orig1);
console.log(copy1);
console.log(orig1);

let orig2 = [
    [1, 2, 3, 4],
    [4, 5],
    [7, 8, 9]
]
let copy2 = bottomRightElementToX(orig2);
console.log(copy2);
console.log(orig2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment