143k views
4 votes
Define a function below called increase_elements_by_x, which takes two arguments - a list of numbers and a single positive number (you might want to call it x). Complete the function such that it returns a copy of the original list where every value is increased by the second argument. For example, given the inputs [1,2,5] and 2, your function should return [3,4,7].

User Ayfer
by
6.2k points

1 Answer

6 votes

Answer:

function

increase_elements_by_x (list, x)

{

var tplist = [];

for (i = 0; i < list.length; i++)

{

tplist[i] = list[i] + x;

print (tplist[i])}

return tplist;

}

var list =[1, 3, 5];

var copyList;

var x = 3;

copyList = increase_elements_by_x (list, x);

print (copyList);

Step-by-step explanation:

Create a list named list with initial data 1,3,5.

Create a function name increase_elements_by_x which takes list and number as argument.Create empty list tplist. Loop through list, for ever index add x to list[index] and save to an empty list tplist. After loop is exited return tplist.

create a variable copylist and set it to increase_elements_by_x. Value returned by increase_elements_by_x will be saved in copylist. print copy list to see content of copy list.

User Zchenah
by
6.6k points