# How to find the root of g[x_] using If?

How can get roots only saved in the list?

Input

g[x_] := x^2 + x - 6;
g /@ Table[i, {i, -10, 10}]
If[g[x] == 0, i, Nothing]


Output

{-3,2}

How can I achieved that kind of output?

xs = Table[i, {i, -10, 10}];
Pick[xs, g /@ xs, 0]
(* {-3, 2} *)

 g[x_] := x^2 + x - 6;

Table[If[g[i] == 0, i, Nothing], {i, -10, 10}]


{-3, 2}

Or

 val = Table[i, {i, -10, 10}];


{-3, 2}

• Can you solve this with function If Or Nothing? Where did I get wrong? Can you point out the mistakes I made here? I would appreciate it if you could do these for me. – kile Oct 26 '19 at 16:24
• @kile Try If[g[#]==0,#,Nothing]&/@Table[i,{i,-10,10}] and try testx[x_]:=If[g[x]==0,x,Nothing];testx/@Table[i,{i,-10,10}] Search and study the help system documentation until you can figure these out so you will be able to use these ideas next time. – Bill Oct 26 '19 at 17:20
• testx[x_]:=If[g[x]==0,x,Nothing];testx/@Table[i,{i,-10,10}] It failed to give the right answer. – kile Oct 27 '19 at 11:40

An alternative approach would be to use Solve:

sol = Solve[g[x] == 0, x];
x /. sol
{-3, 2}

• This is not what I want. Just try each number from -10 to 10. – kile Oct 27 '19 at 5:24