Q:

JavaScript string injection

I have a textarea that lets you input a code and then check it and display its result. I have two functions in JS : 1. that is the code that it is an input in the textarea 2. function that takes the code from textarea and checks it. 
function 1 (code as input)
var searchInsert = function (nums, target) {

    var start = 0;
    var end = nums.length - 1;

    var index = Math.floor((end - start) / 2) + start;

    if (target > nums[nums.length - 1]) {

        index = nums.length;
    }
    else {

        while (start < end) {

            var val = nums[index];

            break;
        }
        if (val == target) {
            result = index;
        }
        else if (target < val) {

            end = index;
        }
        else {

            start = index + 1;
        }

        index = Math.floor((end - start) / 2) + start
    }
    return index;

};

function 2(takes code from textarea and checks it)
function what(nums, target) {
            var s = "var nums = [1, 3, 5, 6]; var target = 5";
            var expression = searchInsert();
            //inject string in function
            var result = eval(expression);
            alert(result);
        }

What I want is to inject somehow the var s = "var nums = [1, 3, 5, 6]; var target = 5"; into the first function so it takes the values from nums and target. How can I do that?

A:

When you call the 1 function, you need to write the parameters:
 function what() {
   var nums = [1, 3, 5, 6]; 
   var target = 5;
   var expression = searchInsert(nums, target);
        //inject string in function
        var result = eval(expression);
        alert(result);
    }

If you want to access the DOM you should use: getElementById('textArea').getValue();

