From 700209dc05a256314b715b0a55c1837d0b8f1fd6 Mon Sep 17 00:00:00 2001 From: Kayalvizhi Rajendran Date: Fri, 31 May 2024 10:54:46 -0500 Subject: [PATCH] COmpleted Studio - More on functions --- .../studio/part-one-find-minimum-value.js | 8 ++--- .../part-three-number-sorting-easy-way.js | 5 +++- .../studio/part-two-create-sorted-array.js | 30 +++++++++++++++++++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/more-on-functions/studio/part-one-find-minimum-value.js b/more-on-functions/studio/part-one-find-minimum-value.js index b537107c03..ee96b7170b 100644 --- a/more-on-functions/studio/part-one-find-minimum-value.js +++ b/more-on-functions/studio/part-one-find-minimum-value.js @@ -1,19 +1,17 @@ //1) Create a function with an array of numbers as its parameter. The function should iterate through the array and return the minimum value from the array. Hint: Use what you know about if statements to identify and store the smallest value within the array. - //Sample arrays for testing: let nums1 = [5, 10, 2, 42]; let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3]; let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0]; //Using one of the test arrays as the argument, call your function inside the console.log statement below. -function findTheSmallestNumber(arr){ +function getSmallestNumber(arr){ let smallestNumber = arr[0]; for (let index = 0; index < arr.length; index++) { if(arr[index] < smallestNumber){ smallestNumber = arr[index]; } - console.log(smallestNumber); } return smallestNumber; -} -console.log(`The smalllest value in the array is ${findTheSmallestNumber(nums1)}`); +}; +console.log("The smallest number in the array is ", getSmallestNumber(nums3)); diff --git a/more-on-functions/studio/part-three-number-sorting-easy-way.js b/more-on-functions/studio/part-three-number-sorting-easy-way.js index 386cde1da8..eff8a8f91d 100644 --- a/more-on-functions/studio/part-three-number-sorting-easy-way.js +++ b/more-on-functions/studio/part-three-number-sorting-easy-way.js @@ -4,5 +4,8 @@ let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3]; let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0]; //Sort each array in ascending order. - +nums1.sort(function(a, b){return a-b}); +console.log(nums1); //Sort each array in decending order. +nums2.sort(function(a, b){return b-a }); +console.log(nums2); \ No newline at end of file diff --git a/more-on-functions/studio/part-two-create-sorted-array.js b/more-on-functions/studio/part-two-create-sorted-array.js index bc362a3101..3c81acb266 100644 --- a/more-on-functions/studio/part-two-create-sorted-array.js +++ b/more-on-functions/studio/part-two-create-sorted-array.js @@ -19,11 +19,41 @@ function findMinValue(arr){ 6) Be sure to print the results in order to verify your code.*/ //Your function here... +function sortArray(arr){ + let sorttedArray = []; + let oldArray = arr; + let minValue; + console.log(oldArray.length); + while(oldArray.length !== 0){ + minValue = findMinValue(arr); + sorttedArray.push(minValue); + oldArray.splice(oldArray.indexOf(minValue),1); + } + return sorttedArray; +} /* BONUS MISSION: Refactor your sorting function to use recursion below: */ +function sortArrayRecursion(arr){ + let sortArray = []; + let value; + if(arr.length <= 1){ + return sortArray.push(arr); + }else { + value = findMinValue(arr); + console.log("min value",value); + sortArray.push(value); + let index = arr.indexOf(value); + arr = arr.splice(index,1); + return sortArray + sortArrayRecursion(arr); + } +} //Sample arrays for testing: +let nums0 = [5]; let nums1 = [5, 10, 2, 42]; let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3]; let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0]; + +console.log(sortArray(nums2)); +//console.log(sortArrayRecursion(nums2)); \ No newline at end of file