-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4221 from Cesar-Ch/add-cesar-solutions
Reto #28 - Javascript
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
Retos/Reto #28 - EXPRESIÓN MATEMÁTICA [Media]/javascript/cesar-ch.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Crea una función que reciba una expresión matemática (String) | ||
* y compruebe si es correcta. Retornará true o false. | ||
* - Para que una expresión matemática sea correcta debe poseer | ||
* un número, una operación y otro número separados por espacios. | ||
* Tantos números y operaciones como queramos. | ||
* - Números positivos, negativos, enteros o decimales. | ||
* - Operaciones soportadas: + - * / % | ||
* | ||
* Ejemplos: | ||
* "5 + 6 / 7 - 4" -> true | ||
* "5 a 6" -> false | ||
*/ | ||
|
||
|
||
function isMathExpression(str) { | ||
const operations = ['+', '-', '*', '/', '%'] | ||
const mathExpression = str.split(' ') | ||
if (mathExpression.length < 3 || mathExpression.length % 2 == 0) { | ||
return false | ||
} | ||
|
||
let isMathExpression = true | ||
mathExpression.map((e, i) => { | ||
if (!Number(e) && i % 2 == 0) { | ||
isMathExpression = false | ||
} | ||
|
||
if (!operations.includes(e) && i % 2 != 0) { | ||
isMathExpression = false | ||
} | ||
} | ||
) | ||
return isMathExpression | ||
|
||
} | ||
|
||
console.log(isMathExpression("3 + 5")) | ||
console.log(isMathExpression("3 a 5")) | ||
console.log(isMathExpression("-3 + 5")) | ||
console.log(isMathExpression("- 3 + 5")) | ||
console.log(isMathExpression("-3 a 5")) | ||
console.log(isMathExpression("-3+5")) | ||
console.log(isMathExpression("3 + 5 - 1 / 4 % 8")) |