Question

Modify the function to return a copy of the given array sorted in ascending order (1,...

Modify the function to return a copy of the given array sorted in ascending order (1, 2, 3, etc). Do not modify the original array.

function copyAndSortNumbers(numbers) {

}

/* Do not modify code below this line */

const original = [1, 7, 3, 5];
const sortedCopy = copyAndSortNumbers(original);

console.log(original, '<-- should be [1, 7, 3, 5]');
console.log(sortedCopy, '<-- should be [1, 3, 5, 7]');

0 0
Add a comment Improve this question Transcribed image text
Answer #1
function copyAndSortNumbers(numbers) {
   result = [...numbers];
   result.sort();
   return result;
}

/* Do not modify code below this line */

const original = [1, 7, 3, 5];
const sortedCopy = copyAndSortNumbers(original);

console.log(original, '<-- should be [1, 7, 3, 5]');
console.log(sortedCopy, '<-- should be [1, 3, 5, 7]');

Add a comment
Know the answer?
Add Answer to:
Modify the function to return a copy of the given array sorted in ascending order (1,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify the function to push the given item onto the end of the array and return...

    Modify the function to push the given item onto the end of the array and return the array. function addItemToArray(array, item) { } const items = addItemToArray([1, 2, 3], 4); console.log(items, '<-- should equal [1, 2, 3, 4]');

  • Modify the function to return the average age of everyone in the people array, rounded to...

    Modify the function to return the average age of everyone in the people array, rounded to the nearest integer. function getRoundedAverageAge(people) { } /* Do not modify code below this line */ const examplePeopleArray = [ { name: 'John', age: 19 }, { name: 'Jack', age: 21 }, { name: 'Jane', age: 22 } ]; console.log(getRoundedAverageAge(examplePeopleArray), '<-- should be 21');

  • using c++ Write a function that returns true if a given integer array is sorted(in ascending...

    using c++ Write a function that returns true if a given integer array is sorted(in ascending order) and false if it is not. The function should perform no more than "size" comparisons. bool isSorted(int A[], int size);

  • Given a sorted (in ascending order) integer array nums of n elements and a target value,...

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a method to perform a binary search for a target value in nums. If target exists, then return its index, otherwise return -1. Analyze the running time, T(n). public int search(int[] nums, int target) { // YOUR CODE GOES HERE } // end search

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • Implement a method to build an AVL tree out of a sorted (ascending order) array of...

    Implement a method to build an AVL tree out of a sorted (ascending order) array of unique integers, with the fastest possible big O running time. You may implement private helper methods as necessary. If your code builds a tree that is other than an AVL tree, you will not get any credit. If your code builds an AVL tree, but is not the fastest big O implementation, you will get at most 12 points. You may use any of...

  • IN MATLAB The function included here is an attempt to sort a given array in ascending...

    IN MATLAB The function included here is an attempt to sort a given array in ascending order. The function does not work as intended. While it correctly moves the highest value to the right-most position, the rest of the array has not finished being sorted. >> arr arr = 40 37 99 4 89 92 80 10 27 34 68 14 >> bubbleSort(arr) ans = 37 40 4 89 92 80 10 27 34 68 14 99 >> function [sorted]...

  • C programing Write a program to sort numbers in either descending or ascending order. The program...

    C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...

  • Modify your asm code further so that you can copy the entire array of numbers (1...

    Modify your asm code further so that you can copy the entire array of numbers (1 to 100) to a different location in the memory (starting from 0x0000_0200h location, i.e. offset of 200h). Lab6.s (MIPS assemblv code .data tmpval: .word 5 globl main .text main # above codes are mandat ry (entry to main function) ######### only modify the c des below ########### # L ad address # initialize N addiu ŞvO, $zero, 1 loopl SW $vO, 0 ($s0) $t0,...

  • Write a templated function sumList that will take in an array of any type and the...

    Write a templated function sumList that will take in an array of any type and the length of the array. It should add all the elements in the array and return the total. Hint: you can declare a variable sum of type T and initialize it like this: T sum {}; The {} are the best way to make sure that numbers get set to 0, a string gets created as empty etc... #include <iostream> using namespace std; //Do not...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT