到目前为止这是我的代码:
var n = 123456789;
var d = n.toString().length;
var digits = [];
var squaredDigits = [];
for (i = d; i >= 1; i--) {
var j = k / 10;
var r = (n % k / j) - 0.5;
var k = Math.pow(10, i);
var result = r.toFixed();
digits.push(result);
}
console.log(digits);
Run Code Online (Sandbox Code Playgroud)
但是当我运行我的代码时,我得到了这个: [9, 1, 2, 3, 4, 5, 6, 7, 8]
如果有人能看到问题或找到更好的解决方案,我将非常感激!
我研究了分隔的延续,目前正在尝试丢弃它们以获得类似于引发异常的效果。
这是给我带来麻烦的原因:
const structure = type => cons => {
const f = (f, args) =>
({["run" + type]: f, [Symbol.toStringTag]: type, [Symbol("args")]: args});
return cons(f);
};
const Cont = structure("Cont")
(Cont => f => Cont(f));
const runCont = tf => k =>
tf.runCont(k);
const reset = tf =>
of(tf.runCont(id));
const shift = f =>
Cont(k => f(k).runCont(id));
const of = x =>
Cont(k => k(x));
const liftM2 = f => tf => tg =>
of(runCont(tf) (x => runCont(tg) (y …
Run Code Online (Sandbox Code Playgroud)javascript continuations haskell functional-programming delimited-continuations
我试图找出一种在迭代数组时进行搜索的方法.我遇到了find()
方法.
这是给出的例子:
var inventory = [
{name: 'apples', quantity: 2},
{name: 'bananas', quantity: 0},
{name: 'cherries', quantity: 5}
];
function findCherries(fruit) {
return fruit.name === 'cherries';
}
console.log(inventory.find(findCherries));
// { name: 'cherries', quantity: 5 }
Run Code Online (Sandbox Code Playgroud)
我需要找到一个动态的水果名称,但我无法弄清楚如何做到这一点.简而言之,我正在尝试做类似的事情:
function findCherries(fruit, fruitName) {
return fruit.name === fruitName;
};
inventory.find(findCherries('cherries'))
//"true is not a function"
Run Code Online (Sandbox Code Playgroud)
有没有办法给出find()
一个参数并找到该参数的匹配基础?如果没有,什么方法允许我动态搜索对象数组?
我想将一个数字拆分成数字(例如4563到4,5,6,3),然后成瘾这个数字.(例如:4 + 5 + 6 + 3 = 18)
我可以单独编写3位数或2位数的代码和...数字,但我不能为每个数字写一个全局代码.
所以这是我的2位数字代码:
var a = 23
var b = Math.floor(a/10); // 2
var c = a-b*10; // 3
var total = b+c; // 2+3
console.log(total); // 5
Run Code Online (Sandbox Code Playgroud)
这是我的3位数字代码:
var a = 456
var b = Math.floor(a/100); // 4
var c = a-b*100; // 56
var d = Math.floor(c/10); // 5
var e = c-d*10; // 6
var total = b+d+e; // 4+5+6
console.log(total); // 15
Run Code Online (Sandbox Code Playgroud)
但我不能写一个代码来处理每个数字.我怎么能为每个数字写一个全局代码?
我尝试使用say-asterpret-as让 Alexa 说出数字中的数字
示例 - 9822 不得读为单词而是“9,8,2,2”
我尝试过的两种方法之一如下:
this.emit(':tell',"Hi "+clientname+" your "+theIntentConfirmationStatus+" ticket is sent to "+ "<say-as interpret-as='digits'>" + clientno + "</say-as>",'backup');
Run Code Online (Sandbox Code Playgroud)
另一件是这样的:
this.response.speak("Hi "+clientname+" your "+theIntentConfirmationStatus+" ticket is sent to "+ "<say-as interpret-as='digits'>" + clientno + "</say-as>");
Run Code Online (Sandbox Code Playgroud)
两者都没有工作,而是在开发一个单独的新功能。
创建由数字中的数字组成的数组的最短方法是什么?
我不想恢复声明一个空数组,然后通过for(a,b,c)
循环遍历数字.
我想要一些更具说服力的东西.理想情况下,例如:
Array.from(143) // => [1, 4, 3]
Run Code Online (Sandbox Code Playgroud) 例如,假设我有数字 345。如何在 javascript 中循环遍历数字中的每个数字并将其提高到连续的 n 次方:即 3^1 + 4^2 + 5^3?