Function that returns if one we can go to the bank with true / false? [closed]

closed. this question is out of scope and is not currently accepting answers.

want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 9 months ago .

improve this question

Exercise: set the function can bank that, receive two parameters, the first is daysweek (string) and the second current hour( number), the function should return true, only if the bank 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 go against string, but I used the tip of colleague Sergio in another question that I had doubts and thought it would solve. But now when I put the following Test in the console possoIrAoBanco ('Friday', 10) it returns FALSE even if the date being Friday and the schedule being 10. Would anyone know how to explain what is causing this error?

Author: Stéphanie Verissimo, 2019-08-29

10 answers

Hello, I did so and found it simpler:

function possoIrAoBanco(diaDaSemana, horaAtual){

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

  return diaDaSemana != naoPode && horaAtual >= 9 && horaAtual <= 15;
}
 2
Author: Thais Marques, 2019-12-06 21:03:57

The resolution looked like this:

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

In the case, as the bank time is a number you would not need to assign to a variable as you were doing with the weekdays that is assigned to a string.

 3
Author: Stéphanie Verissimo, 2019-11-21 16:52:57

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 in which will be associated with string daysfunction bank and the other values will be discarded, if you test any other parameter other than Friday will fail.

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

ArrayOf returns the position of the item in the array or -1 If the item is not contained in the array, then we just need to check if the daysweek 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
Author: Vinicius Fernandes, 2019-08-29 04:16:00

This 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
Author: Robson Firmino, 2019-11-28 02:48:29

I did like this:

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));
 0
Author: William Felipe, 2019-11-25 16:00:16

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
Author: Fabio Macieira, 2019-11-29 02:06:01
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
Author: luiz adolfo Tucunduva, 2020-05-21 17:15:18

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 the logic inside a parent key and deny everything, saying that whatever is true in the end is false=)

 -1
Author: user190564, 2020-05-22 21:45:32
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;
}
 -2
Author: Ricardo, 2019-11-30 16:00:53
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 the one above.

 -2
Author: Erick Oliveira, 2020-05-22 02:41:49