JS | parseInt()更改stringNumber& stringWord |解?

时间:2017-10-11 15:20:48

标签: javascript string numbers parseint typeof

我对parseInt()有疑问。当我从输入(代码中为car)中取一个字符串annualInput时,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>

4 个答案:

答案 0 :(得分:0)

尝试使用

let numberValue = +getValue || getValue;

而不是

let numberValue = parseInt(getValue);

答案 1 :(得分:0)

您可以使用 Number() constructor 代替parseInt()来解析您的号码。

let numberValue = Number(getValue) || getValue;

它只会为数字字符串提供一个数字,并为其他字符串返回NaN

<强>演示:

&#13;
&#13;
console.log(Number("20"));
console.log(Number("car"));
&#13;
&#13;
&#13;

答案 2 :(得分:0)

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

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

如果getValue == '123123'它将被转换为int。

如果getValue == '23234x'它仍然是一个字符串。

答案 3 :(得分:0)

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

您可以替换此行:

let numberValue = parseInt(getValue);

这一行:

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

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