Skip to content

Commit

Permalink
Merge pull request mouredev#4221 from Cesar-Ch/add-cesar-solutions
Browse files Browse the repository at this point in the history
Reto mouredev#28 - Javascript
  • Loading branch information
kontroldev authored Jul 21, 2023
2 parents 649a34a + edb52fa commit d71e9ef
Showing 1 changed file with 44 additions and 0 deletions.
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"))

0 comments on commit d71e9ef

Please sign in to comment.