Program will not run, what is wrong with my function? Returning boolen values


#1

i am trying to write a program that returns a boolean value after it has sorted through an array, false if it is not sorted and true if it is, what am i doing wrong?function sortArray() {

    var userInput = readlineSync.question['Enter an array'];
    for (var i = 0; i < userInput.length - 1; i++){
        if (userInput[i] > userInput[i+1]){
            return false; }
        else {
            return true; }
    }
}
sortArray();

#2

Please try this. Good luck.

var userInput = readlineSync.question('Enter an array');


#3

From the syntax in your question
it is clear that you are working with node.js

Codecademy uses pure Javascript...
google search
node.js vs javascript site:stackoverflow.com

In a separate Post i will give you the Function Talk
so that you know what a parameter and argument is all about.

Here i give you a search criteria for the return-statement.
if the return-statement is executed you will directly leave your function
== the Book ==
javascript return statement site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return
=discussions=
javascript return statement site:stackoverflow.com



#4

the FUNCTION talk

var myFunc = function( param1, param2) {
       //Begin of  anonymous FUNCTION-BODY
       //VARIABLE -myFunc- has an -anonymous function- assigned
       //this -anonymous function- has 2 PARAMETERS param1 and param2
       //param1 and param2 PARAMETERS are used 
       //as -local- VARIABLES throughout the FUNCTION-BODY

      console.log( param1 + " and " + param2 ) ;

      //End of anonymous FUNCTION-BODY
};

If you want to call/execute the anonymous function
you will have to add a pair of parentheses to the variable myFunc
like
myFunc();
As the anonymous function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren");

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**


#5

This directly returns after the first element wouldn't it be better to return true after the loop is done?


#6

normal brackets instead of squared brackets.
var userInput = readlineSync.question(‘Enter an array’);