Hello, does anyone know how to solve it? I tried it with a thousand formulas and nothing
> function fakeBinary2(string) {
The function called 'fakeBinary' receives as an argument a string with numbers and must return a string where numbers less than 5 are replaced with '0' and numbers equal to or greater than 5 with '1'
// Por ej:
// fakeBinary2('5627') devuelve "1101"
// fakeBinary("729") devuelve "101"
// Tu código aca:
} I don't know how to deal with the statement I think, I tried making an array of "string" but each element remains as a string so I don't know how to indicate "if less than 5 replace with 1" and so on. I hope it is understood
function fakeBinary2(string) {
string = string.split(",");
for (let i=0; i< string,length; i++ ){
if (string[i] == "2"||"3"||"4"){
string.replace(i, "0")
} else if (string[i] == "5"||"6"||"7"||"8"||"9"){
string.replace(i, "1")}
} return string;
}
Fast answer:
Explanation
Hello, I recommend you to refactor your code, taking into account this:
and not like that
Important
An important issue to clarify, I am comparing a string with a number
it's like saying
This does not give an error because JavaScript inside converts the string "7" to a number 7 since there is a comparison of "greater than".
You can make it simpler as follows:
With the operator
...
you pass each character of string individually. So ifstring = "0925"
, it[...string]
is equivalent to[ "0", "9", "2", "5" ]
.With
map
you generate a new array in which if the characters are greater than"5"
they are replaced by"1"
else"0"
.Finally, all the elements of the array are concatenated in a string with
join
.I hope you are well, I note that you are perhaps starting with programming, remember you must always first make a flowchart and/or design of the solution to be implemented in the face of your problem.
I share with you an example code of how to solve your problem/statement, I provide it as a guide and basis for you to resolve doubts, this is not the only or the best solution; but that is the great thing about programming, there are many solutions to a problem.
Greetings, and I hope to continue studying this great art that is programming!
console.log(fakeBinary2("178452")) runs only one test and the other does not xq