英文:
replace the value from last index of array
问题
Sure, here's the translated code portion:
我有一个数组,看起来像这样
let = ["1", "0", "0", "0", ".", "0", "0"]
我想要改变最后一个索引的值,然后不断移动到0的索引
例如,我想要输入5、3、2。输出应该变成
```javascript
updatedArray = ["1", "0", "0", "5", ".", "3", "2"]
这可行吗?
我尝试过这个来检查0的索引
const stringValueArray = stringValue.split('');
const emptyStringIndices: number[] = [];
stringValueArray.forEach((element, index) => {
if (element === '0') {
emptyStringIndices.push(index);
}
});
const highestIndex = Math.max(...emptyStringIndices);
stringValueArray[highestIndex] = value;
const newValue = stringValueArray.join('');
return newValue;
希望这对你有帮助。
英文:
i have an array that looks like this
let = ["1", "0", "0", "0", ".", "0", "0"]
i want to change the value of last index and then constantly move to the indexes of 0's
for example i want to input 5,3,2. the output should become
updatedArray = ["1", "0", "0", "5", ".", "3", "2"]
is this possible?
i tried this to check for the indexes of 0
const stringValueArray = stringValue.split('');
const emptyStringIndices: number[] = [];
stringValueArray.forEach((element, index) => {
if (element === '0') {
emptyStringIndices.push(index);
}
});
const highestIndex = Math.max(...emptyStringIndices);
stringValueArray[highestIndex] = value;
const newValue = stringValueArray.join('');
return newValue;
答案1
得分: 0
新答案,
仔细考虑一下,如果你反转 data
数组,然后对其使用 map()
,如果有元素就返回 input.pop()
(使用 || c
作为备用),然后再次反转它。
let data = ["1", "0", "0", "0", ".", "0", "0"],
input = ["5", "3", "2"];
data.reverse();
data = data.map((c, i) => (c === "0") ? (input.pop() || c) : c).reverse();
console.log(data)
原始答案:
你可以创建一个循环,在其中保持一个最后更改的索引的标志。
然后检查该索引是否等于 0
,如果是的话,用更改后的数组的最后一个项目替换它,使用 pop()
,这样我们可以将 input.length
用作 while
条件。
let data = ["1", "0", "0", "0", ".", "0", "0"],
input = ["5", "3", "2"];
let lastIndex = data.length - 1;
while (input.length && lastIndex > 0) {
if (data[lastIndex] == 0) {
data[lastIndex] = input.pop();
}
lastIndex--;
}
console.log(data)
英文:
New answer,
On second thought, if you reverse
the data
array, you can map()
over it, return input.pop()
if there is some (|| c
) as fallback, and then reverse it again.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let data = ["1", "0", "0", "0", ".", "0", "0"],
input = [ "5", "3", "2" ];
data.reverse();
data = data.map((c, i) => (c === "0") ? (input.pop() || c) : c).reverse();
console.log(data)
<!-- end snippet -->
Original answer:
You can create a loop, in which you hold a flag for the last changed index.
Then check if that index is equal to "0"
, if so, replace it with the last item in the changed array, using pop()
so we can use input.length
as the while
condition
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let data = ["1", "0", "0", "0", ".", "0", "0"],
input = [ "5", "3", "2" ];
let lastIndex = data.length - 1;
while (input.length && lastIndex > 0) {
if (data[lastIndex] == 0) {
data[lastIndex] = input.pop();
}
lastIndex--;
}
console.log(data)
<!-- end snippet -->
答案2
得分: 0
跟踪原始数字和输入的新数字。这是必要的,因为您可能需要输入402以获得1004.02,而您不希望代码混淆并将1000.42作为结果返回,因为它没有意识到您输入的零需要保留。
假设输入始终包含小数点后面跟着两位小数。
然后,通过将原始数字与输入的数字组合来获取结果,注意在返回结果之前重新插入小数点。
如果输入了太多数字,忽略任何多余的数字。
英文:
Keep track of the original digits and the new digits entered. This is necessary, because you may need to enter 402 to get 1004.02, and you would not want the code to get confused and give 1000.42 as the result because it did not realise that the zero you entered needs to stay.
It is assumed that the input always contains the decimal place followed by two decimal numbers.
Then, get the result by combining the original digits with the entered digits, taking care to re-insert the decimal point before returning the result.
If too many digits are entered, ignore any excess digits.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
class Keypad {
constructor(s) {
this.origDigits = [...s].filter(c =>c !=='.');
this.maxKeyPresses = this.origDigits.filter(c => c==='0').length;
this.keyPresses = [];
}
keyPressed(n) {
if(this.keyPresses.length < this.maxKeyPresses) this.keyPresses.push(n);
}
get val() {
let digits = [
...this.origDigits.slice(0, this.origDigits.length - this.keyPresses.length),
...this.keyPresses
];
digits.splice(-2, 0, '.');
return digits.join('');
}
}
let k = new Keypad('1000.00');
console.log(k.val);
k.keyPressed(5);
console.log(k.val);
k.keyPressed(3);
console.log(k.val);
k.keyPressed(2);
console.log(k.val);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论