0

I am attempting to write a javascript file that has a insertion sort function, a function to check a sorted array and return true or false, and an insertion sort function that works from the end of the array index to the beginning. Here is the code i have

function insertionSort(arr) { 
    for(var i = 1; i < arr.length; i++) { 
        var val = arr[i]; var j; for(j = i; j > 0 && arr[j-1] > val; j--) {
            arr[j] = arr[j-1]; } arr[j] = val; }
    }
function reverseInsertionSort(arr) { 
    for(var i = arr.length; i >1; i--) 
    { var val = arr[i]; var j;
        for(j = i; j > 0 && arr[j-1] > val; j--)
            { arr[j] = arr[j-1]; } arr[j] = val;
            } }
var length = Math.floor(Math.random()*100)+1;
var arr = new Array();
for(let i = 0; i < length; i++) {
  arr.push(Math.floor(Math.random()*10000)+1);
}
console.log(arr);
//function sortCheck(arr) {
//for( var i = 0 ; i < arr.length; i++){
//  if(arr[i]>rr[i+1]){
//      return false
//  }
//}
//return true}


var sortedArr = insertionSort(arr);
console.log(sortedArr);
console.log("And with reverse \n");
var reverseSortedArr = reverseInsertionSort(arr);
console.log(reverseSortedArr);
//console.log(sortCheck(sortedArr));

The issue I am having right now is that sortedArr is undefined when output with console.log, it appears that the issue is that my function is "undefined" but seeing how i define it above, i dont understand how that is.

2
  • 3
    Your functions have no return statements. Commented Feb 7, 2019 at 3:11
  • The formatting in this code makes my brain hurt. Why do you have multiple statements piled up in a single line? Commented Feb 7, 2019 at 3:27

3 Answers 3

2

Your insertionSort function doesn't return a value, it modifies the array passed as an argument. Instead of var sortedArr = insertionSort(arr), just call insertionSort(arr) and then do console.log(arr).

Sign up to request clarification or add additional context in comments.

1 Comment

thank you! its been a while since i've used javascript
0

You have to return arr from the function. It is not returning anything that's why you are getting undefined

function insertionSort(arr) { 
    for(var i = 1; i < arr.length; i++) { 
        var val = arr[i]; var j; for(j = i; j > 0 && arr[j-1] > val; j--) {
            arr[j] = arr[j-1]; } arr[j] = val; }
   return arr; }
function reverseInsertionSort(arr) { 
    for(var i = arr.length; i >1; i--) 
    { var val = arr[i]; var j;
        for(j = i; j > 0 && arr[j-1] > val; j--)
            { arr[j] = arr[j-1]; } arr[j] = val;
            } return arr}
var length = Math.floor(Math.random()*100)+1;
var arr = new Array();
for(let i = 0; i < length; i++) {
  arr.push(Math.floor(Math.random()*10000)+1);
}
console.log(arr);



var sortedArr = insertionSort(arr);
console.log(sortedArr);
console.log("And with reverse \n");
var reverseSortedArr = reverseInsertionSort(arr);
console.log(reverseSortedArr);
//console.log(sortCheck(sortedArr));

2 Comments

It's considered a bad practice to modify an array and return it. You should either return a new array without modifying the argument, or modify the argument. Not both - this can cause nasty side effects.
@Our_Benefactors Array.prototype.sort(), and Array.prototype.reverse() would suggest otherwise. That's nonsense. Document behavior and return the reference if you want, it's useful for chaining and other common language patterns.
0

Make sure you return the array from the function(s). Since you currently are not, assigning the function to a variable would not yield any particular value.

function insertionSort(arr) { 
    for(var i = 1; i < arr.length; i++) { 
        var val = arr[i]; 
        var j; 
        for(j = i; j > 0 && arr[j-1] > val; j--) {
            arr[j] = arr[j-1]; 
        } 
        arr[j] = val; 
     } 
    return arr
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.