Function that returns if one can go to the database with true/false?

Asked

Viewed 5,627 times

1

EXERCISE: Set the function possibleIrAoBanco, take two parameters, the first is diaDaSemana (string) and the second horaAtual(number), the function must return true, only if the database is open. Remember that you can do what is needed using Return without making use of if/Else.

I MADE THE FOLLOWING CODE:

function possoIrAoBanco( diaDaSemana, horaAtual){
 var horaFuncionamentoBanco = (9, 10,11,12,13,14,15);
  var diasFuncionamentoBanco = ('segunda-feira', 'terça-feira', 'quarta-feira','quinta-feira', 'sexta-feira');
 return horaAtual === horaFuncionamentoBanco &&diaDaSemana=== diasFuncionamentoBanco;
}

HOWEVER: Before was appearing the error of the solution to go against string, but I used the tip of colleague Sergio in another issue that I had doubt and thought it would solve. But now when I put the following test on the console can be FALSE('Friday', 10) it returns FALSE even if the date is Friday and the time is 10. Would anyone know what’s causing this mistake?

  • Function possoIrAoBranco(diaDaSemana, horaAtual) { var diasFechados = ('Saturday', 'Sunday'); Return diaDaSemana !== diasFechados && horaAtual >= 9 && horaAtual <= 15; } I believe this test is from Digital House. If so, see that he will not accept the code with var diasFechados = ('Saturday', 'Sunday'); but only with ('Saturday, 'Sunday''). I don’t know if this was something anticipated, but every other working day is accepted in lowercase letters but the weekend is not. I believe it’s a mistake, but it left me for a long time thinking that the logic of function was wrong.

  • Function possoIrAoBanco(dayDaSemana, timeAtual){ Return timeAtual >= 9 && timeAtual <= 15 && dayDaSemana != " Saturday" && dayDaSemana != " Sunday" } Mine was like this and I managed to pass... the problem is the accent of Saturday and Sunday with bad letter.

10 answers

3

The resolution was thus:

function possoIrAoBanco( diaDaSemana, horaAtual){
var diasFuncionamentoBanco = ('segunda-feira', 'terça-feira', 'quarta-feira','quinta-feira', 'sexta-feira');
 return 9 <=horaAtual<=15&&diaDaSemana=== diasFuncionamentoBanco;
}

In this case, as the bank time is a number it would not need to assign to a variable as it was doing with the diaDaSemana that is assigned to a string.

  • For me this will always return false...??? this is not a function in_array... are strings, when you check the type, example: typeof ('segunda-feira', 'terça-feira', 'quarta-feira','quinta-feira', 'sexta-feira') only returns a 'string', then its === will only check if it is of the type string... and if it is equal to the whole structure its diaDaSemana... which will be a value that would be of an array, then it makes no sense at all. Unless you change those parentheses to square brackets. [ ], and scroll through each position... [+]

  • [+] or return if the type is different from -1, return (diasFuncionamentoBanco.indexOf(diaDaSemana) !== -1)

2

In javascript the string type can only store a value and not a "list" of values as you are doing.

You said that your example worked by testing with the Friday parameter is because in the "list" you created Friday is the last reported value to which the string will be associated daysOperationBank and the other values will be dropped, if you test any other parameter other than Friday you will fail.

To solve this rather than define daysOperationBank as string would be better as array and test whether the value passed as argument is contained within the array using the method arrayOf(item).

arrayOf returns the position of the item in the array or -1 if the item is not contained in the array, so we just need to check if the day is different from -1.

function possoIrAoBanco(diaDaSemana, horaAtual) {
  const diasFuncionamentoBanco = ['segunda-feira', 'terça-feira', 'quarta-feira', 'quinta-feira', 'sexta-feira'];

  const diaDeFuncionamento = diasFuncionamentoBanco.indexOf(diaDaSemana) !== -1;

  const horarioDeFuncionamento = horaAtual >= 9 && horaAtual <= 15;

  return diaDeFuncionamento && horarioDeFuncionamento;
}

console.log(`domingo as 8: ${possoIrAoBanco('domingo', 8)}`);
console.log(`segunda as 9: ${possoIrAoBanco('segunda-feira', 9)}`);
console.log(`terça as 10: ${possoIrAoBanco('terça-feira', 10)}`);
console.log(`quarta as 20: ${possoIrAoBanco('quarta-feira', 20)}`);
console.log(`quinta as 12: ${possoIrAoBanco('quinta-feira', 12)}`);
console.log(`sexta as 15: ${possoIrAoBanco('sexta-feira', 15)}`);
console.log(`sabado as 14: ${possoIrAoBanco('sabado', 14)}`);

2

This one worked for me...

var segunda = "segunda-feira";
var terca = "terca-feira";
var quarta = "quarta-feira";
var quinta = "quinta-feira";
var sexta = "sexta-feira";
var sabado = "sábado";
var domingo = "domingo";
function possoIrAoBanco(diaDaSemana, horaAtual) {
  return diaDaSemana != sabado && diaDaSemana != domingo && horaAtual >= 9 && horaAtual <= 15;
}

2


Hello, I did so and found it simpler:

function possoIrAoBanco(diaDaSemana, horaAtual){

  var naoPode = ('sábado' || 'domingo');

  return diaDaSemana != naoPode && horaAtual >= 9 && horaAtual <= 15;
}

0

I did so:

function possoIrAoBanco(diaDaSemana, horaAtual){
  var dia = ('segunda-feira', 'terça-feira', 'quarta-feira', 'quinta-feira', 'sexta-feira');
  return diaDaSemana === dia && horaAtual >= 9 && horaAtual <= 15;
}

console.log(possoIrAoBanco('sabado', 11));

-1

Have to declare every day of the week to work.

function possoIrAoBanco (diaDaSemana, horaAtual) {
  var segunda = "segunda-feira";
var terca = "terca-feira";
var quarta = "quarta-feira";
var quinta = "quinta-feira";
var sexta = "sexta-feira";
var sabado = "sábado";
var domingo = "domingo";

  return diaDaSemana != sabado
      && diaDaSemana != domingo 
      && horaAtual >= 9 
      && horaAtual <= 15;
} 

-1

function possoIrAoBanco (diaDaSemana, horaAtual) {

var segunda= 'Segunda';
var terca = 'Terça';
var quarta = 'Quarta';
var quinta = 'Quinta';
var sexta = 'Sexta';
var sabado = 'Sábado';
var domingo =  'Domingo';

return (diaDaSemana !== sabado) &&( diaDaSemana !== domingo) && (horaAtual >= 9 && horaAtual <= 15) ;
} 


possoIrAoBanco( 'Segunda', 10);
possoIrAoBanco('Terça', 18) ;
possoIrAoBanco('Sábado', 11);
possoIrAoBanco('Domingo',13) ;

-1

Hello, after 1 hour of attempts this solution worked super well.

function possoIrAoBanco(diaDaSemana, horaAtual){
    return (!(diaDaSemana == "Sábado" || diaDaSemana == "Domingo") && (horaAtual >= 9 && horaAtual <= 15));
}

All you have to do is put logic inside a mother key and deny everything, saying that what is true in the end is false =)

-2

function possoIrAoBanco(diaDaSemana, horaAtual) {
    return diaDaSemana != "Sábado" && diaDaSemana != "Domingo" && horaAtual >= 9 && horaAtual <=15;
}

I did the same exercise at Digital House, and the only answer he took for granted was this one above.

-2

function possoIrAoBanco(diaDaSemana,horaAtual) {

var diasFuncionamentoBanco = ('segunda-feira', 'terça-feira', 'quarta-feira','quinta-feira', 'sexta-feira');

  var sabado = "sábado"

  var domingo = "domingo"

  return (horaAtual > 0  && horaAtual <= 15)  && (diaDaSemana != sabado && diaDaSemana != domingo ) || diaDaSemana ==  diasFuncionamentoBanco;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.