danoseun
danoseun

Reputation: 69

Why is the program returning undefined?

Write a mySort function which takes in an array of integers, and should return an array of the inputted integers sorted such that the odd numbers come first and even numbers come last. So: mySort([90, 45, 66, 'bye', '100.5']) should return [45, 66, 90, 100].

Here is my code:

function mySort(array) {
  var strArray = [];
  var oddArray = [];
  var evenArray = [];
  var sortedArray = [];
  var arrayLength = array.length
  for (var i = 0; i <= arrayLength; i++) {
    if (array[i] === 'string') {
      strArray = array[i].push();
    }
    if (Math.floor().array[i] % 2 === 0) {
      evenArray = array[i].push();
    }
    if (Math.floor().array[i] % 2 !== 0) {
      oddArray = array[i].push();
    }
  }

  sortedArray = sort(oddArray) + sort(evenArray);
}

console.log(mySort[90, 45, 66, 'bye', 100.5]);

Upvotes: 0

Views: 225

Answers (3)

A. Vision
A. Vision

Reputation: 1

With a simple improvement on @llama 's code, expected result is returned.

We just need to sort oddArray and evenArray individually.

function mySort(nums) {
  'use strict'
    var array = nums
    var strArray = [];
    var oddArray = [];
    var evenArray = [];
    var sortedArray = [];
    var arrayLength = array.length;
    for (var i = 0; i < arrayLength; i++) {
        if (typeof array[i] === 'string') {
            strArray.push(array[i]);
        }
        else if (Math.floor(array[i]) % 2 === 0) {
            evenArray.push(Math.floor(array[i]));
        }
        else if (Math.floor(array[i]) % 2 !== 0) {
            oddArray.push(Math.floor(array[i]));
        }
    }
// sort oddArray
    oddArray.sort(function (a, b) { return a - b; });
// sort evenArray
    evenArray.sort(function (a, b) { return a - b; });
// make an array from both of them.
    sortedArray = oddArray.concat(evenArray);
    return sortedArray;
}

Upvotes: 0

aaron
aaron

Reputation: 43098

Several errors.

  1. You didn't return:

    function mySort(array) {
        // ...
        return sortedArray;
    }
    
  2. You should pass the parameter to Math.floor:

    Math.floor(array[i]);
    
  3. You should pass the parameter to array.push:

    strArray.push(array[i]);
    evenArray.push(array[i]);
    oddArray.push(array[i]);
    
  4. You should concat:

    sortedArray = oddArray.concat(evenArray);
    

Now it at least runs.

  1. You should use typeof: (sorts string)

    if (typeof array[i] === 'string') {
        // ...
    }
    
  2. You should use else if: (removes string)

    if (typeof array[i] === 'string') {
        // ...
    }
    else if (Math.floor(array[i]) % 2 === 0) {
        // ...
    }
    else if (Math.floor(array[i]) % 2 !== 0) {
        // ...
    }
    
  3. Your for loop should end earlier: (removes undefined)

    for (var i = 0; i < arrayLength; i++) {
        // ...
    }
    
  4. You should push the floor:

    evenArray.push(Math.floor(array[i]));
    oddArray.push(Math.floor(array[i]));
    
  5. Finally, sort it in order using a comparator:

    sortedArray.sort(function (a, b) { return a - b; })
    

Solution:

function mySort(array) {
    var strArray = [];
    var oddArray = [];
    var evenArray = [];
    var sortedArray = [];
    var arrayLength = array.length;
    for (var i = 0; i < arrayLength; i++) {
        if (typeof array[i] === 'string') {
            strArray.push(array[i]);
        }
        else if (Math.floor(array[i]) % 2 === 0) {
            evenArray.push(Math.floor(array[i]));
        }
        else if (Math.floor(array[i]) % 2 !== 0) {
            oddArray.push(Math.floor(array[i]));
        }
    }

    sortedArray = oddArray.concat(evenArray);
    sortedArray.sort(function (a, b) { return a - b; });
    return sortedArray;
}

console.log(mySort([90, 45, 66, 'bye', 100.5])); // [45, 66, 90, 100]

Upvotes: 2

llama
llama

Reputation: 2537

  • You have no return statement. JavaScript function syntax doesn't implicitly return the last expression, except to return undefined if no explicit return is present.

  • You should .concat() the arrays. The + doesn't do what you seem to want.

    return sort(oddArray).concat(sort(evenArray));
    
  • This condition is incorrect:

    if(array[i] === 'string'){
    
  • You need typeof there to check the type of the member.

    if(typeof array[i] === 'string'){
    
  • Your .push() calls are wrong. I guess you meant this:

    evenArray.push(array[i]);
    
  • Your recursive calls should be using mySort() instead of sort().

  • strArray and sortedArray are essentially being ignored.

  • Math.floor() expects an argument.

     Math.floor(array[i])
    
  • Your loop condition is wrong. It should be i < arrayLength, not <=

  • Your recursion has no escape clause

  • Your initial call to mySort is not using parentheses to invoke it. It should be:

    console.log(mySort([/*...array items...*/]));
    
  • Your if conditions should be using else. If it's a string, you don't need to test it again, assuming it is to be excluded. If it's not even, then you know it's odd, based on your criteria, so the last condition isn't needed.

  • You apparently want to convert strings to numbers if possible, and converted to an integer. So instead of the typeof check, you could go ahead and convert it using parseInt or + or Number, and check if it's NaN, and if so, pass it over, and if not, make sure it's converted to an integer before testing it for odd/even.


Just to note, arrow functions do have an implicit return when the function body consists of a single expression.


Here's a working example with the above corrections:

function mySort(array) {
    var oddArray = [];
    var evenArray = [];
    var arrayLength = array.length;
    for(var i = 0; i < arrayLength; i++) {
        var n = Math.floor(Number(array[i]));
        if (isNaN(n)) {
            continue;
        }
        
        if (n % 2 === 0 ){
            evenArray.push(n);
        } else {
            oddArray.push(n);
        }
    }

    return oddArray.length < 1 ? evenArray :
           evenArray.length < 1 ? oddArray : 
           mySort(oddArray).concat(mySort(evenArray));
}


console.log(mySort([90, 45, 66, 'bye', 100.5]));

Upvotes: 2

Related Questions