forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
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 mouredev#5063 from flaviovich/main
Reto mouredev#31 - Javascript
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
Retos/Reto #31 - EL ÁBACO [Fácil]/javascript/flaviovich.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,40 @@ | ||
/* | ||
* Crea una función que sea capaz de leer el número representado por el ábaco. | ||
* - El ábaco se representa por un array con 7 elementos. | ||
* - Cada elemento tendrá 9 "O" (aunque habitualmente tiene 10 para realizar operaciones) | ||
* para las cuentas y una secuencia de "---" para el alambre. | ||
* - El primer elemento del array representa los millones, y el último las unidades. | ||
* - El número en cada elemento se representa por las cuentas que están a la izquierda del alambre. | ||
* | ||
* Ejemplo de array y resultado: | ||
* ["O---OOOOOOOO", | ||
* "OOO---OOOOOO", | ||
* "---OOOOOOOOO", | ||
* "OO---OOOOOOO", | ||
* "OOOOOOO---OO", | ||
* "OOOOOOOOO---", | ||
* "---OOOOOOOOO"] | ||
* | ||
* Resultado: 1.302.790 | ||
*/ | ||
|
||
const getNumber = (value, index) => value * "1".padEnd(7 - index, "0"); | ||
|
||
const abacus = (array) => { | ||
return array.reduce((acc, val, i) => { | ||
const value = val.indexOf("---"); | ||
return (acc += getNumber(value, i)); | ||
}, 0); | ||
}; | ||
|
||
const array = [ | ||
"O---OOOOOOOO", | ||
"OOO---OOOOOO", | ||
"---OOOOOOOOO", | ||
"OO---OOOOOOO", | ||
"OOOOOOO---OO", | ||
"OOOOOOOOO---", | ||
"---OOOOOOOOO", | ||
]; | ||
console.clear(); | ||
console.log(abacus(array)); |