简体   繁体   English

我需要一个函数来搜索数组并返回最后一个字符串后的最后一组数字?

[英]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.

相关问题 我需要编写一个遍历数字数组的函数,并在其数组中返回奇数和偶数。 - I need to write a function that loops through an array of numbers, and returns the odd & even numbers in it's array. 排序字符串数组但数字最后 - Sorting array of string but numbers last 我必须创建一个函数,它返回传入的数组的最后一个元素 - I must create a function, that returns the last element of an array that is passed in 遍历一个数组并将修改后的项目复制到另一个数组中,仅返回最后一组修改的重复 - Looping through an array and copying the modified items into another array returns only repetitions of the last set of modifications 遍历字符串数组-停留在最后一个字符串上 - Looping through array of strings - stay on last string 如何从字符串中提取包含字符和数字的最后一位数字 - How to extract last digit from string which contains chars and numbers 我正在尝试使用.replace()替换特定的字符串。 第一次和最后一次搜索将被替换,但第二次则不会 - I am trying to replace a particular string using .replace(). First and last searches are getting replaced but not second one 使用jquery迭代数组只返回最后一个对象 - Iterating through array using jquery returns only last object 我需要编写一个 function “checkArray”,它接收一个随机数数组并返回大于 5 的数字之和 - I need to write a function “checkArray” which receives an array of random numbers and return the sum of the numbers bigger than 5 如何对姓氏进行排序、格式化电话号码并返回包含对象数组中的值的字符串? - How do I sort the last names , format phone numbers, and return a string containing values from an array of objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM