JS | parseInt() 更改 stringNumber 和 stringWord |解决方案?

JS | parseInt() changes stringNumber & stringWord | solution?

我对 parseInt() 有疑问。当我从输入(代码中的 annualInput)中获取一个字符串,例如 car 时,parseInt() 将其更改为一个数字。我只想将键入的数字(字符串)更改为数字,这样我就可以使用 typeof numberValue == 'number'。感谢您对代码的任何帮助。

function wage() {
    let input = document.getElementById('annualInput').value;
    let output = document.getElementById('resultMonthly');
      
    // annual wage devided by 12 months
    let resultMonthly = input / 12;
    let showMonthly = 'Your monthly wage: ' + resultMonthly.toFixed(2);
    
    // monthly wage devided by 20 days
    let resultDaily = resultMonthly / 20;
    let showDaily = 'Your daily wage: ' + resultDaily.toFixed(2);
    
    // daily wage devided by 8 hours
    let resultHourly = resultDaily / 8;
    let showHourly = 'Your hourly wage: ' + resultHourly.toFixed(2);
    
    input = +input;

     //check if input is empty and if string is number
     if (input) {
         if (isNaN(input) !== true) {
            document.getElementById('resultMonthly').innerHTML = showMonthly + ' $';
            document.getElementById('resultDaily').innerHTML = showDaily + ' $';
            document.getElementById('resultHourly').innerHTML = showHourly + ' $';
         } 
         else {
        document.getElementById('resultMonthly').innerHTML = 'Please, type your annual wage';
        document.getElementById('resultDaily').innerHTML = "e.g 125000";
        document.getElementById('resultHourly').innerHTML = "and click the button 'check'";
         }
     }

    
    // switch option for wage occupation
    let storeWage;
    let dev = document.getElementById('dev').value;
    
    switch(dev) {
        case "junior developer":
            storeWage = "40 000 $";
            break;
        case "mid developer":
            storeWage = "75 000 $";
            break;
        case "senior developer":
            storeWage = "120 000 $";
            break;
        default:
            storeWage = "Choose one category of developers";
    }
    document.getElementById('devOutput').innerHTML = "Estimated wage for " + dev + ": " + storeWage;
    
    
      //  alert typeof shows number when typed either string or number
    let getValue = document.getElementById("annualInput").value
    document.getElementById("devOutput").innerHTML = getValue;
    let numberValue = parseInt(getValue);
    alert(typeof numberValue);
   
    if (numberValue !== '') {
        // this if doesn't work even with string and number
         if (typeof numberValue == 'number')
            document.getElementById('result').innerHTML = "great" + numberValue;
        else alert("try again")
    } else {
        document.getElementById('result').innerHTML = 'Please, type your annual wage';
    }
    
    
}
<!DOCTYPE html>
<html>
<head>
<title>Wage App</title>
    <meta charset="utf-8">
        <title>Issue App</title>   
        <link rel="stylesheet" type="text/css" href="wageup.css">
</head>
<body>
    <div class="container">
        <div class="jumbotron">
            
            <!-- the title and desc -->
            <h1 class="display-4 text-center">WageApp</h1>
            <p class="h6 text-center" id="paragraphTitle">This is a simple App for calculating wage by year, month, day and hour</p>
            <hr class="my-4">
            
            <!-- input-form   -->
            <form>
                <div class="form-group">
                    <label for="annualInput">Your annual wage</label>
                        <input id="annualInput" type="annualData" class="form-control" aria-describedby="hep" placeholder="Annual wage in $">
                    <small id="help" class="form-text text-muted">We'll never share your wage with anyone else.</small>
                </div>
            
            <!-- choose developers -->
                <div class="form-group">
                    <label for="categoryDevelopers">Your category of developers</label>
                        <input id="dev" type="categoryDevelopers" class="form-control" aria-describedby="hep" placeholder="Your developer">
                    <small class="form-text text-muted">Choose: junior developer, mid developer, senior developer</small>
                </div>
               
                <button onclick="wage()" type="button" class="btn btn-primary btn-sm" id="annualInput">Check</button>
                            
            </form>
            
        </div>
        <div class="alert alert-success" role="alert" id="resultMonthly">
        </div>
        <div class="alert alert-info" role="alert" id="resultDaily">
        </div>
        <div class="alert alert-warning" role="alert" id="resultHourly">
        </div>
        <div class="alert alert-danger" role="alert" id="devOutput">
        </div>
    </div>
     </div>
    
    
    <!-- JS -->
    <script type="text/javascript" src="wageUp.js"></script>
    <!-- Jquery -->
        <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
    <!-- Bootstrap -->
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

</body>

</html>

尝试使用

let numberValue = +getValue || getValue;

而不是

let numberValue = parseInt(getValue);

您可以使用 Number() constructor 而不是 parseInt() 来解析您的号码。

let numberValue = Number(getValue) || getValue;

它只会给数字字符串一个数字,return NaN 其他字符串。

演示:

console.log(Number("20"));
console.log(Number("car"));

那么,如果它是数字,您只想转换为 int 吗?如果是这样,请使用正则表达式:

var value = /^\d+$/.test(getValue) ? parseInt(getValue, 10) : getValue;

如果getValue == '123123'它将被转换为一个整数。

如果getValue == '23234x'它将保留为字符串。

一个选项是使用正则表达式。

您可以替换此行:

let numberValue = parseInt(getValue);

这一行:

let numberValue = /^\d+$/.test(getValue) ? parseInt(getValue, 10) : getValue;

您可以使用 parseInt 和 10 作为基数参数。