使用随机选择从数组返回函数值 [英] Returning function value from array using random selection

查看:60
本文介绍了使用随机选择从数组返回函数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在返回数组内部函数的计算值时遇到了一些问题.并希望获得解决方案方面的帮助以及有关解决此类问题的更优雅方法(即解决问题的更合理方法)的建议

Been running into some problems with returning the computed value of a function that is inside an array. and would appreciate any help in solutions as well as advice about more elegant ways of approaching this type of problem (i.e. a more logical way to have solved this)

我正在尝试创建一个程序,该程序生成两个随机数字和一个随机运算符以应用于所述数字.

I'm trying to create a program that generates two random digits and a random operator to apply to said digits.

var num1 = Math.floor(Math.random() * 20 + 1);
console.log(num1);
var num2 = Math.floor(Math.random() * 1000 + 21);
console.log(num2);

我最初是这样设置我的随机运算符的:

I originally set my random operator this way:

var ops = ["+", "-", "*", "/"] 

我尝试使用Math.random函数为ops数组选择一个随机索引号,但是遇到各种错误,因此我开始搜索并在此处找到一些有用的建议:通过索引访问非数字对象属性?),所以我改回使用4个函数组成的数组,每个函数都有一个返回值,该返回值根据随机运算符计算随机数(num1和num2).

I tried to use a Math.random function to select a random index number for the ops array, but was getting all kinds of errors, so I started searching and found some helpful advice here: How to use Javascript to ask the user random math questions?, but couldn't figure out a way to retrieve a random value from the object,(cf. Access non-numeric Object properties by index?) so I changed back to using an array of 4 functions, each of which has a return value that computes the random numbers (num1 and num2) based on the random operator.

我已使所有工作正常(即测试了我的随机数函数是否工作,甚至看到我的newOp变量每次都从ops数组返回一个不同的函数),但这是我无法获得的程序要做的:返回函数的计算值.当我

I've gotten everything to work (i.e. have tested my random number functions are working, and even see that my newOp variable is returning a different function from the ops array each time), but here's what I can't get the program to do: return the computed value of the function. When I

alert(newOp)

我希望它提醒功能的 value 而不是功能本身.(例如,警报-> 20,而不是我当前得到的内容:->函数a(){返回(num1 + num2)}

I want it to alert the function's value instead of the function itself. (e.g. alert -> 20 instead of what I'm currently getting: --> function a(){return (num1 + num2)}

这是我的代码.欢迎所有咨询和有识之士!只是想学习.

Here's my code. All advice and insight welcome! Just trying to learn.

var num1 = Math.floor(Math.random() * 20 + 1);
console.log(num1);
var num2 = Math.floor(Math.random() * 1000 + 21);
console.log(num2);
var ops = [
    function a(){return (num1 + num2)}, 
    function b(){return (num1 - num2)}, 
    function c(){return (num1 * num2)},
    function d(){return (num1 / num2)}];

var problem = function () {
    var random = Math.floor(Math.random()* 4 + 0);
    var newOp = ops[random];
    alert(newOp);
};
problem();

推荐答案

我认为您只需将其更改为:

I think you just need to change it to:

alert(newOp());

您的数组包含对函数的引用,但您没有在调用它们.

Your array contains references to the functions, but you're not invoking them.

这篇关于使用随机选择从数组返回函数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆