Need help adding elements to an array from another array

Viewed 62

I have two arrays - a, with 5 values and s which is empty. What I have to do is to find the smallest value in a, add it to s and after adding it to s, to delete it from a. Here is my code:

            class Sorting {
                constructor () {
                    let getArr = document.getElementById('t');
                }

            findMin () {
                let a = [23, 30, 9, 10, 26];
                let s = [];

                for (let i = 0; i < a.length; i++) {
                    let min = i;
                    for (let j = i + 1; j < a.length; j++) {
                    if  (a[j] < a[min]) {
                    min = j;
                    }
                }
                
                s.push(a[min]);

                a.splice(min, 1);
                
                    if (i !== min) {
                    let x = a[i];
                    a[i] = a[min];
                    a[min] = x;                    
                    }
            
                }
                
                console.log(s)
                console.log(a)
            }
        }
            
        function main() {
        let p = new Sorting ();
        p.findMin();
    }

What I'm unable to do is to make my program delete the element from a properly after adding it to s.

If I delete the line a.splice(min, 1) and just leave s.push(a[min]), I get all the values from a into s. If I leave them both however, that's what I get as a result:

s: [9, 23, 30]

a: [10, 26]

And I want to get:

s: [9, 10, 23, 26, 30]

a: []

Can anyone please tell me why I get this and how can I fix it? Any help would be greatly appreciated.

3 Answers

You could simply check the value and splice the array with the found index and take this array for pushing.

const
    a = [23, 30, 9, 10, 26],
    s = [];

while (a.length) {
    let i = 0;
    for (let j = 1; j < a.length; j++) {
        if (a[j] < a[i]) i = j;
    }
    s.push(...a.splice(i, 1));
}

console.log(a);
console.log(s);

let a = [25,2,5,28,10,32];
let s = [];
var previousVal;
var index;

//least value find
for (let i = 0; i< a.length - 1; i++){
    if(i == 0){
        previousVal = a[i];
    }
    var result = previousVal - a[i+1];
    if(result > 0){
        //previousVal is grater than a[i+1]
        previousVal = a[i+1];
        index = i+1;
    }
}

console.log("Index = " + index + " and Number = " + previousVal);
a.splice(index, 1);
s.push(previousVal);    
console.log(a);
console.log(s);

Hope this helps. I approached using different method to find the least value first and index in the array then removed that from array 'a' and put it in array 's'. You could remove 'previousValue' at the end and just use 'index'. Just push it before splicing. Like,

s.push(a[index]);
a.splice(index,1);

How about slightly different approach with this problem? somthing like

let a = [23, 30, 9, 10, 26];
let s = [];
let sorted = a.slice().sort((a, b) => a - b);
s[0] = sorted[0]; //our s is now smallest number
a.splice(a.findIndex(elem => elem == s[0]), 1)
console.log(a);
console.log(s);

Here we finding smallest number in array by sorting it from smallest to largest. To delete it we finding index of our smallest number with .findIndex() and delete it with .splice

Related