Increase readability, update code style and optimise for modern JavaScript (#273)

* Remove loops on recursive example, increase readability and optimise

* Update style and optimise

* Update 01_loop_sum_reduce_version.js

Update style

* Update style

* Rename identificators

* Rename identificators

* Convert shift to splice
This commit is contained in:
Timur Sevimli
2024-12-07 16:48:14 +03:00
committed by GitHub
parent c06ad954f0
commit 45bf3c086d
4 changed files with 46 additions and 56 deletions

View File

@@ -1,46 +1,39 @@
"use strict"; 'use strict';
/** /**
* Finds the index of the element with the smallest value in the array * Finds the index of the element with the smallest value in the array
* @param {Array} array Source array * @param {Array} array Source array
* @returns {number} Index of the element with the smallest value * @returns {number} Index of the element with the smallest value
*/ */
function findSmallestIndex(array) { const findSmallest = (arr) => {
var smallestElement = array[0]; // Stores the smallest value let [smallestElement] = arr;
var smallestIndex = 0; // Stores the index of the smallest value let smallestIndex = 0;
for (let i = 1; i < arr.length; i++) {
for (var i = 1; i < array.length; i++) { const el = arr[i];
if (array[i] < smallestElement) { if (el >= smallestElement) continue;
smallestElement = array[i]; smallestElement = el;
smallestIndex = i; smallestIndex = i;
}
} }
return smallestIndex; return smallestIndex;
} };
/** /**
* Sort array by increment * Sort array by increment
* @param {Array} array Source array * @param {Array} array Source array
* @returns {Array} New sorted array * @returns {Array} New sorted array
*/ */
function selectionSort(array) { const selectionSort = (arr) => {
var sortedArray = []; const size = arr.length;
var copyArray = array.slice(); const result = new Array(size).fill(0);
var length = array.length; for (let i = 0; i < size; i++) {
const smallestIndex = findSmallest(arr);
for (var i = 0; i < length; i++) { const [smallestElement] = arr.splice(smallestIndex, 1);
// Finds the smallest element in the array result[i] = smallestElement;
var smallestIndex = findSmallestIndex(copyArray);
// Adds the smallest element to new array
sortedArray.push(copyArray.splice(smallestIndex, 1)[0]);
} }
return result;
return sortedArray; };
}
const sourceArray = [5, 3, 6, 2, 10]; const sourceArray = [5, 3, 6, 2, 10];
const sortedArray = selectionSort(sourceArray); const sortedArray = selectionSort(sourceArray);
console.log("Source array - ", sourceArray); // [5, 3, 6, 2, 10] console.log('Source array - ', sourceArray); // [5, 3, 6, 2, 10]
console.log("New sorted array - ", sortedArray); // [2, 3, 5, 6, 10] console.log('New sorted array - ', sourtedArray); // [2, 3, 5, 6, 10]

View File

@@ -1,20 +1,17 @@
'use strict';
/** /**
* Finds smallest element of an aray * Finds smallest element of an aray
* @param {Array} arr array for searching * @param {Array} arr array for searching
* @return {number} index of the smallest element in array * @return {number} index of the smallest element in array
*/ */
const findSmallest = ( arr ) => { const findSmallest = (arr, smallest = arr[0], smallestIndex = 0, i = 1) => {
let smallest = arr[0]; if (arr.length <= i) return smallestIndex;
let smallestIndex = 0; const curr = arr[i];
let arrLen = arr.length; if (curr < smallest) {
smallest = curr;
for ( let i = 0; i < arrLen; i++ ) { smallestIndex = i;
if ( arr[i] < smallest ) { }
smallest = arr[i]; return findSmallest(arr, smallest, smallestIndex, i + 1);
smallestIndex = i;
}
}
return smallestIndex;
}; };
/** /**
@@ -22,12 +19,15 @@ const findSmallest = ( arr ) => {
* @param {Array} arr An array of numbers * @param {Array} arr An array of numbers
* @return {Array} New sorted array * @return {Array} New sorted array
*/ */
const selectionSort = ( arr ) => { const selectionSort = (arr, result = []) => {
if ( !arr.length ) return []; if (arr.length > 0) {
let smallest = arr.splice( findSmallest( arr ), 1 ); const smallestIndex = findSmallest(arr);
return smallest.concat( selectionSort( arr ) ); const [smallest] = arr.splice(smallestIndex, 1);
result.push(smallest);
return selectionSort(arr, result);
}
return result;
}; };
let arr = [5, 3, 6, 2, 10]; const arr = [5, 3, 6, 2, 10];
console.log(selectionSort(arr));
console.log( selectionSort(arr) );

View File

@@ -5,10 +5,6 @@
* @param {Array} array Array of numbers * @param {Array} array Array of numbers
* @returns {number} Sum of the numbers * @returns {number} Sum of the numbers
*/ */
function sumReduce(array) { const sumReduce = (array) => array.reduce((prev, cur) => prev + cur, 0);
return array.reduce(function(prev, cur) {
return prev + cur;
});
}
console.log(sumReduce([1, 2, 3, 4])); // 10 console.log(sumReduce([1, 2, 3, 4])); // 10

View File

@@ -1,13 +1,14 @@
"use strict"; 'use strict';
/** /**
* Sums values in the array by recursive * Sums values in the array by recursive
* @param {Array} array Array of numbers * @param {Array} array Array of numbers
* @returns {number} Sum of the numbers * @returns {number} Sum of the numbers
*/ */
function sumRecursive(arr) { const sumRecursive = (arr = []) => {
if (arr.length == 0) return 0; if (!arr.length) return 0;
return arr[0] + sumRecursive(arr.slice(1)); return arr[0] + sumRecursive(arr.slice(1));
} };
console.log(sumRecursive([1, 2, 3, 4])); // 10 console.log(sumRecursive([1, 2, 3, 4])); // 10