'Function returning undefined, I know it's to do with the non-return but I am struggling to solve

I have a function that takes a variable called (compactArray) (there is another function to produce and return a compact array that works fine)

I know it's because my function is not returning any value, but I am struggling to solve the return = undefined.

the code is below

let a = null;
let b = null;
let operand = null;
let total = null;



function testGrab (compactArray) {
    if(compactArray == typeof(Number) && a == null) {
     return a = compactArray;
    } else if (compactArray == typeof(Number) && a !== null){
        return b = compactArray;
    } else if (compactArray == typeof(String)){
        return operand = compactArray; 
    }
  return total
};


const compactArray = 467; 

console.log(testGrab(compactArray));
console.log(a)

I want this function to take the compactArray returned from a different function and assign it to the correct variable a,b,operand.

is this possible, or would I need to make a function to return the relevant if statement, and then create another function to assign the returned value to the correct variable?

I know it's because my function is not returning anything but I am stumped at the moment.



Solution 1:[1]

if you want to check whether compactArray's type is a number:

change this statement "if(compactArray == typeof(Number) && a == null)"

to this statement "if("number" == typeof(compactArray) && a == null)"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dawit