javascript中的逻辑增强练习

在JavaScript中不能使用正规表达式也不能用isNaN方法获取字符串中所有的数字
str=‘A1B2C52’; 输出1,2,52


let str = 'A1B2C52';
let result = '';
for (let i = 0; i < str.length; i++) {
    const charCode = str.charCodeAt(i);
    if (charCode >= 48 && charCode <= 57) {
        result += str[i];
    } else {
        if (result !== '') {
            console.log(result);
            result = '';
        }
    }
}
if (result !== '') {
    console.log(result);
}

该回答引用chatgpt:


var str = 'A1B2C52';
var result = [];

for (var i = 0; i < str.length; i++) {
  var char = str.charAt(i);
  var num = parseInt(char);

  if (!isNaN(num)) {
    result.push(num);
  }
}

console.log(result); // 输出 [1, 2, 5, 2]