function add(x, y) {
return x + y;
}
add(2, 3); //5
add(2, 3); //6
add('2', '3'); //'23'
2 + 3 = 5
Error si no se usan números
0.1 + 0.2 = 0.3
function add(x, y) {
return x + y;
}
2 + 3 = 5
Error si no se usan números
0.1 + 0.2 = 0.3
function add(x, y) {
if((typeof x && typeof y) !== 'number'){
throw new Error('Params must be a number.')
}
return x + y;
}
2 + 3 = 5
Error si no se usan números
0.1 + 0.2 = 0.3
function add(x, y) {
var result;
if((typeof x && typeof y) !== 'number'){
throw new Error('Params must be a number.')
}
result = x + y;
if (parseInt(result) !== result) {
result = parseFloat(result.toFixed(1));
}
return result;
}
function add(x, y) {
var result;
if((typeof x && typeof y) !== 'number'){
throw new Error('Params must be a number.')
}
result = x + y;
if (parseInt(result) !== result) {
result = parseFloat(result.toFixed(1));
}
return result;
}
//Expect add(2, 3) to equal 5
//Expect add() to throw an error if x/y are not numbers
//Expect add(0.1, 0.2) to equal 0.3
function add(x, y) {
var result;
if((typeof x && typeof y) !== 'number'){
throw new Error('Params must be a number.')
}
result = x + y;
if (parseInt(result) !== result) {
result = parseFloat(result.toFixed(1));
}
return result;
}
//Expect add(2, 3) to equal 5
expect(add(2, 3)).toBe(5);
//Expect add() to throw an error if x/y are not numbers
//Expect add(0.1, 0.2) to equal 0.3
function add(x, y) {
var result;
if((typeof x && typeof y) !== 'number'){
throw new Error('Params must be a number.')
}
result = x + y;
if (parseInt(result) !== result) {
result = parseFloat(result.toFixed(1));
}
return result;
}
//Expect add(2, 3) to equal 5
expect(add(2, 3)).toBe(5);
//Expect add() to throw an error if x/y are not numbers
expect(add(2, 'test')).toThrow();
//Expect add(0.1, 0.2) to equal 0.3
function add(x, y) {
var result;
if((typeof x && typeof y) !== 'number'){
throw new Error('Params must be a number.')
}
result = x + y;
if (parseInt(result) !== result) {
result = parseFloat(result.toFixed(1));
}
return result;
}
//Expect add(2, 3) to equal 5
expect(add(2, 3)).toBe(5);
//Expect add() to throw an error if x/y are not numbers
expect(add(2, 'test')).toThrow();
//Expect add(0.1, 0.2) to equal 0.3
expect(add(0.1, 0.2)).toBe(0.3);