'I need a function which searches through an array and returns the last set of numbers after the last string?
I need a function called getLastNumbers which searches through an array and returns the last set of numbers after the last operator.
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]);
Solution 1:[1]
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]);
Solution 2:[2]
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]))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
