试图弄清楚如何将字符串数组转换为数字数组
trying to figure out how to convert an array of strings to an array of numbers
我在弄清楚如何将字符串数组转换为整数数组时遇到问题。
例如以下数组:
['137','364','115','724']
我基本上要做的是删除和 return 每个元素中的最大数字,并将其添加到第二大数字、第三大数字等等,具体取决于数字的长度。 (数组中的元素总是相同的长度)
例如:
第一个最大的数字是 7,(从每个元素中删除最大的数字,留下 [13,34,11,24])
剩下的第二大数字是 4(再次从每个元素中删除最大数字 [1,3,1,2])
剩下的第三大数字是 3(此时没有更多数字可以删除)
7 + 4 + 3
这是我目前拥有的:
function sW(his) {
// Write your code here
for (var i = 0; i < history.length; i++ ){
var something = history[i].split('')
console.log(something)
console.log(Math.max(parseInt(something)))
console.log(parseInt(something))
}
}
到目前为止这还行不通,因为当我尝试解析 Int/return 每个索引处的最大值时,它总是 return 第一个数字。希望得到一些帮助,谢谢!
这是一种解决方案:
function slotWheels(arr) {
// Sort all the numbers in the array
// ['137','364','115','724'] -> ['731', '643', '511', '742']
const sorted = arr.map(e => [...e].sort((a, b) => b - a).join(''));
let sum = 0;
for(let i = 0; i < sorted[0].length; i++) {
// find out the max number of each numbers at the same position, and add it to sum
sum += Math.max(...sorted.map(e => e[i]));
}
return sum;
}
console.log(slotWheels(['137','364','115','724']));
strs = ['1234','4231','1423','3241']
result = strs
.map(str => str.split(''))
.map(arr => arr.sort((a, b) => b - a))
.reduce(
(acc, item) => acc.map((num, j) => Math.max(num, item[j])),
new Array(strs[0].length).fill(0)
)
.reduce((acc, num) => acc + num, 0)
console.log(result);
我认为这个可以帮助:-
function slotWheels(history) {
// Write your code here
if (history[0] && history[0].length > 0) {
let maxLength = history[0].length;
let sumArray = [];
for (var j = 0; j < maxLength; j++) {
let max = Number.MIN_SAFE_INTEGER;
let maxArray = [];
for (var i = 0; i < history.length; i++ ){
var something = Math.max(... history[i].split('').map (str => {return parseInt(str)}));
history[i] = ('' + history[i]).replace (something, '');
maxArray.push (something);
}
sumArray.push (Math.max (... maxArray));
}
var returnSum = 0;
for (let i = 0; i < sumArray.length; i++) {
returnSum += sumArray[i];
}
return returnSum;
} else {
return 0;
}
}
我在弄清楚如何将字符串数组转换为整数数组时遇到问题。
例如以下数组:
['137','364','115','724']
我基本上要做的是删除和 return 每个元素中的最大数字,并将其添加到第二大数字、第三大数字等等,具体取决于数字的长度。 (数组中的元素总是相同的长度)
例如:
第一个最大的数字是 7,(从每个元素中删除最大的数字,留下 [13,34,11,24]) 剩下的第二大数字是 4(再次从每个元素中删除最大数字 [1,3,1,2]) 剩下的第三大数字是 3(此时没有更多数字可以删除)
7 + 4 + 3
这是我目前拥有的:
function sW(his) {
// Write your code here
for (var i = 0; i < history.length; i++ ){
var something = history[i].split('')
console.log(something)
console.log(Math.max(parseInt(something)))
console.log(parseInt(something))
}
}
到目前为止这还行不通,因为当我尝试解析 Int/return 每个索引处的最大值时,它总是 return 第一个数字。希望得到一些帮助,谢谢!
这是一种解决方案:
function slotWheels(arr) {
// Sort all the numbers in the array
// ['137','364','115','724'] -> ['731', '643', '511', '742']
const sorted = arr.map(e => [...e].sort((a, b) => b - a).join(''));
let sum = 0;
for(let i = 0; i < sorted[0].length; i++) {
// find out the max number of each numbers at the same position, and add it to sum
sum += Math.max(...sorted.map(e => e[i]));
}
return sum;
}
console.log(slotWheels(['137','364','115','724']));
strs = ['1234','4231','1423','3241']
result = strs
.map(str => str.split(''))
.map(arr => arr.sort((a, b) => b - a))
.reduce(
(acc, item) => acc.map((num, j) => Math.max(num, item[j])),
new Array(strs[0].length).fill(0)
)
.reduce((acc, num) => acc + num, 0)
console.log(result);
我认为这个可以帮助:-
function slotWheels(history) {
// Write your code here
if (history[0] && history[0].length > 0) {
let maxLength = history[0].length;
let sumArray = [];
for (var j = 0; j < maxLength; j++) {
let max = Number.MIN_SAFE_INTEGER;
let maxArray = [];
for (var i = 0; i < history.length; i++ ){
var something = Math.max(... history[i].split('').map (str => {return parseInt(str)}));
history[i] = ('' + history[i]).replace (something, '');
maxArray.push (something);
}
sumArray.push (Math.max (... maxArray));
}
var returnSum = 0;
for (let i = 0; i < sumArray.length; i++) {
returnSum += sumArray[i];
}
return returnSum;
} else {
return 0;
}
}