/* 斐波那契数列
*
*/
function fib(n) {
if(n == 0) return 0;
let a1 = 0, a2 =1;
for(let i = 1; i < n; i++){
[a1, a2] = [a2, a1 + a2];
}
return a2;
}
console.log(fib(3));
// [2,4,5,6] --> 9
// 从数组中找出两数相加等于给定值,返回两数在数组中的索引
function indexes(nums, target){
let i = nums.length;
while(i > 1){
let n1 = nums.pop(), // 取数组最后一个元素,作为其中一个数值
n2 = target - n1; // 另一数值
if(nums.indexOf(n2) > -1){
return [nums.length, nums.indexOf(n2)]
}
i--;
}
}
console.log(indexes([], 9));
// 简单的模板字符串替换
let template = '{{name}}很厉害,才{{ age }}岁';
let context = {name: '张三', age: 23};
function render(template, context){
return template.replace(/{{(.*?)}}/g, (match, key) => context[key.trim()])
}
console.log(render(template, context));
console