[英]I need a function which searches through an array and returns the last set of numbers after the last string?
I need a function call getLastNumbers which searches through an array and returns the last set of numbers after the last operator.我需要一个函数调用 getLastNumbers,它搜索数组并返回最后一个运算符之后的最后一组数字。
for example:例如:
[1, 1, 1, 1, 1, 1, "+", 2, 2, 2, 2, 2, 2, "*", 3, 3, 3, 3, 3, "-", 6, 6, 6, 6] Returns 6666
[1, 1, 1, 1, "+", 6, 6, 6, 6, 6, 6, "*", 9, 9, 9, 9, 9, "-", 6, 6, 6, 6, "+", 3, 3, 3] Returns 333
[7, "*", 7, "-", 6, "+", 3, "+", 6, "+", 3, 3, 3, 3, 3, "+", 6, "-", 5, 5, 5, 5, 5, 5, "*", 9, 9] Returns 99
[5, "*", 5] returns 5
To clarify, I will never know how many numbers or operators the user may input.澄清一下,我永远不会知道用户可以输入多少个数字或运算符。
See my long winded attempt below attempt below:在下面的尝试中查看我冗长的尝试:
function getLastNumber(arr){
let number = arr.filter(i => i === '*' || i === '/' || i === '+' || i === '-' ).length;
let counter = 0;
let arrayLength = arr.length;
let lastOpIndex = [];
for(let i = 0; i < arr.length; i++) {
if(arr[i] === '*' || arr[i] === '+' ||arr[i] === '-' ||arr[i] === '/'){
counter++;
if (counter === number) {
lastOpIndex.push(arr.indexOf(arr[i]));
}
}
}
let lastOpNum = parseInt(lastOpIndex.toString());
let startNum = lastOpNum + 1;
let mathNum = (arrayLength - lastOpNum) - 1;
let finalArray = [];
for(let i = startNum; i < arr.length; i++) {
finalArray.push(arr[i]);
}
let strResult = finalArray.toString().split(',').join('');
let finalResult = parseInt(strResult);
return finalResult;
}
getLastNumber([1, "+", 3, 3, 3, 3, 3, "+", 6, "+", 9, "*", 6, 6, 6, 3, 6, 6, 6, "-", 5, "*", 9, 9, 9, 9, 9, 9, 9, '/', 6, 8, 9, 9, '*', 8, 8, 8, 7]);
As the question is open again i will extend on my comment.由于问题再次开放,我将扩展我的评论。 I think they way you're trying to achieve this is a bit to complicated.
我认为他们试图实现这一目标的方式有点复杂。
Here is my approach just turning the problem arround and starting with the last index instead of the first one.这是我的方法,只是将问题转向并从最后一个索引而不是第一个索引开始。 As soon as I find an operator I just break the loop and return the saved values in reversed order.
一旦我找到一个运算符,我就打破循环并以相反的顺序返回保存的值。
function getLastNumber(arr){ let ret = []; for(let i = arr.length - 1; i >= 0 ; i--) { if(arr[i] !== '*' && arr[i] !== '+' && arr[i] !== '-' && arr[i] !== '/') { ret.push(arr[i]); } else { break; } } return ret.reverse().toString(); } console.log(getLastNumber([1, "+", 3, 3, 3, 3, 3, "+", 6, "+", 9, "*", 6, 6, 6, 3, 6, 6, 6, "-", 5, "*", 9, 9, 9, 9, 9, 9, 9, '/', 6, 8, 9, 9, '*', 8, 8, 8, 7])); console.log(getLastNumber([1, 1, 1, 1, "+", 6, 6, 6, 6, 6, 6, "*", 9, 9, 9, 9, 9, "-", 6, 6, 6, 6, "+", 3, 3, 3])); console.log(getLastNumber([7, "*", 7, "-", 6, "+", 3, "+", 6, "+", 3, 3, 3, 3, 3, "+", 6, "-", 5, 5, 5, 5, 5, 5, "*", 9, 9])); console.log(getLastNumber([5, "*", 5]))
function filterNumbers(arr){ let a = arr.lastIndexOf("*"); // find last index of * let b = arr.lastIndexOf("+"); // find last index of + let c = arr.lastIndexOf("-"); // find last index of - let d = arr.lastIndexOf("/"); // find last index of / let ind = Math.max(a, b, c, d); //find the max index console.log(ind); let newarr = arr.splice(ind+1,arr.length); //splice array console.log(newarr); return newarr; } filterNumbers([1, 1, 1, 1, 1, 1, "+", 2, 2, 2, 2, 2, 2, "*", 3, 3, 3, 3, 3, 6, 6, 6, 6,1]); filterNumbers([1, 1, 1, 1, 1, 1, "+", 2, 2, 2, 2, 2, 2, "*", 3, 3, 3, 3,"/", 3, 6, 6, 6, 6,1]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.