如何在我的基本javascript计算程序中使用断言?

时间:2017-04-14 06:30:14

标签: javascript html exception-handling assertions

我正在尝试将断言实现到我的程序中(它计算用户输入维度的框的音量)来检查表达式,例如用户输入是否真的是一个数字,以及它是否大于零。我创建了一个函数assert,它接受要测试的表达式,如果有错误,它需要显示的消息,但它对我不起作用。我会编写调用断言函数的代码行,如assert((typeof length ==' number'),'长度必须是数字');但是当我这样做时,该计划根本不起作用。如果我添加console.assert((typeof length ==' number'),'长度必须是数字'); ,程序运行,但它没有做我需要它做的错误检查。我只是开始学习如何进行js异常处理,所以我甚至不确定我是否实现了这个权利呢?



function calculate(){
   'use strict';
   //declare variables used to calculate volume of the box
   var volume;
		//Bullet #4
    
	   var length = document.getElementById('length').value;
	   console.assert( (typeof length == 'number'), 'The length must be a number');
	   console.assert( (length > 0), 'The length must be larger than 0.');
	   
	   var width = document.getElementById('width').value;
	   console.assert( (typeof width == 'number'), 'The width must be a number');
	   console.assert( (width > 0), 'The width must be larger than 0.');
	   
	   var height = document.getElementById('height').value;
	   console.assert( (typeof height == 'number'), 'The height must be a number');
	   console.assert( (height > 0), 'The height must be larger than 0.');
	  
	   //calculate the volume
	   volume = length * width * height;
	   volume = volume.toFixed(2);
	   console.assert( (!isNaN(volume)), 'The volume is not a number.');
	   
	   //display the volume
	   document.getElementById('volume').value = volume;
	
	 return false;
	
}
//Bullet #4
function assert(expression, message){
	if (!expression) throw {name: 'Assertion Exception', message: message};
}

   function init(){
	   'use strict';
	   document.getElementById('theForm').onsubmit = calculate;
   }
   window.onload = init;

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>box calculator</title>
    <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link rel="stylesheet" href="css/styles.css">
</head>
<body>
    <!-- box.html -->
    <form action="" method="post" id="theForm">
        <fieldset>
            <p>Use this form to calculate the volume of a box.</p>
			<div><label for="length">Length</label><input type="text" name="length" id="length" required></div>
			<div><label for="width">Width</label><input type="text" name="width" id="width" required></div>
			<div><label for="height">Height</label><input type="text" name="height" id="height" required></div>
			<div><label for="volume">Volume</label><input type="text" name="volume" id="volume"></div>
			<div><input type="submit" value="Calculate" id="submit"></div>
        </fieldset>
    </form>
    <script src="js/box.js"></script>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

在断言之前快速console.log()会发现任何DOM.value的类型都是字符串。

快速解决方法是将Number()应用于这些字符串(lengthheightwidth)以使其成为数字

注意:将Number()应用于非数字的字符串genererates NaN,其类型为Number,因此您还需要更改断言语句。 我建议每种措施采用以下格式:

var length = Number(document.getElementById('length').value);
console.assert(!isNaN(length), 'The length must be a number');    
console.assert( (length > 0), 'The length must be larger than 0.');