Skip to content

Commit

Permalink
COmpleted Studio - More on functions
Browse files Browse the repository at this point in the history
  • Loading branch information
KayalvizhiRajendran committed May 31, 2024
1 parent 37597ca commit 700209d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
8 changes: 3 additions & 5 deletions more-on-functions/studio/part-one-find-minimum-value.js
Original file line number Diff line number Diff line change
@@ -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));
Original file line number Diff line number Diff line change
Expand Up @@ -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);
30 changes: 30 additions & 0 deletions more-on-functions/studio/part-two-create-sorted-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));

0 comments on commit 700209d

Please sign in to comment.