Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reto #28 - javaScript #5562

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
*/

const tuplaOperaciones = ['+','-','*','/','%']

const expresionMatematica = (expresion) =>{
const ToArray = expresion.split(' ')

for(let i=0;i<=ToArray.length-3;i+=2){

if(ToArray.length<3) return false

if(isNaN(Number(ToArray[i]))){
return false
}
if(!tuplaOperaciones.includes(ToArray[i+1])){
return false
}
if(isNaN(Number(ToArray[i+2]))){
return false
}
return true
}
}

console.log(expresionMatematica("5 + 6 / 7 - 4"))//---->true
console.log(expresionMatematica("5 * 6 / 7 + 4"))//---->true
console.log(expresionMatematica("5 * 6 / -7 + 4 % 3"))//---->true
console.log(expresionMatematica("-5 * 6 / 7 + 4 % 3"))//---->true
console.log(expresionMatematica("5 a 6")) //---->false
console.log(expresionMatematica("a * 6")) //---->false
console.log(expresionMatematica("5 x 6")) //---->false
console.log(expresionMatematica("5 * b")) //---->false