How can i execute each function with his KeyWord to excute it | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can i execute each function with his KeyWord to excute it

const CMDS = [ "Time", "Date", "Battery", "Evaluate Math Simple Expressions(EMSE)", "Calendar", "Help" ]; const fun = [ "time()", "date()", "battery()", "EMSE()", "Calendar()", "help()" ]; var cmd = cap; let used = false; for(let i=0;i<CMDS.length;i++){ if(cmd.toLowerCase()==CMDS[i].toLowerCase()){ used = true; break; } For example: User_input = "Time"; It should get the keyword time, and it works perfectly, but how I code this to call the time() function and show it to the user

18th Dec 2018, 1:51 AM
Juan David Padilla Diaz
Juan David Padilla Diaz - avatar
6 Answers
+ 2
Yes, but the program recognize the keyword as a string not a function. Thats why it doesnt work. To call them you can use window[keyword]() Like my first example. Or look at this one https://code.sololearn.com/WLtS5DadHYX0/?ref=app
19th Dec 2018, 5:01 PM
Taste
Taste - avatar
+ 1
For web ? Are the functions in the global scope ? Then use this. window[user_input]()
18th Dec 2018, 4:26 AM
Taste
Taste - avatar
+ 1
Yeah, if those function (time, date, battery, etc) in a global scope. Not inside a class or other function. Then you can use my method above
19th Dec 2018, 4:47 PM
Taste
Taste - avatar
+ 1
type "time" inside the form without quotation marks. Then click the button. It'll call the function named time from the code
19th Dec 2018, 5:02 PM
Taste
Taste - avatar
0
Taste take a look of the code $(function() { // user = prompt("Type your name"); //while (user === null || user === '') { // user = prompt("Type your name"); //} $("#btn").on("click", function() { cap = $("#inp").val(); $("#inp").val(""); if (cap !== "") { user_input = $('<p class="user-container"></p>').text(cap); $(".main-container").append(user_input); const CMDS = [ "Time", "Date", "Battery", "Evaluate Math Simple Expressions(EMSE)", "Calendar", "Help" ]; const fun = [ "time()", "date()", "battery()", "EMSE()", "Calendar()", "help()" ]; var cmd = cap; let used = false; for(let i=0;i<CMDS.length;i++){ if(cmd.toLowerCase()==CMDS[i].toLowerCase()){ used = true;
19th Dec 2018, 4:15 PM
Juan David Padilla Diaz
Juan David Padilla Diaz - avatar
0
Taste the idea of this program is to run a function typing a keyword. Example: Input: time Output: 11:50am/pm The program recognized the keyword time and call the function time. That's what I want to do, the problem is that it takes the keyword but it doesn't call the function
19th Dec 2018, 4:51 PM
Juan David Padilla Diaz
Juan David Padilla Diaz - avatar