Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

110
Visualizações
Adding constraints of a specific data type in class declaration in javascript

I have the following code exercise:

Create class Intern with next fields: Name(String) Surname(String) laziness(number from 20 to 50) Curiosity (number from 0 to 100) Skill (number from 0 to 100) Irresponsibility (float number from 0.0 to 1.0) For this class create method that calculate “mark” for intern, that is calculated by formula

Question: How can I add constraints to the original data type in a class? like int a, but in range from 20 to 50. or string. Here is my code without constraints:

class Intern {
constructor(name, surname, laziness, curiosity, skill, irresponsibility) {
    this.name = name
    this.surname = surname
    this.laziness = laziness
    this.curiosity = curiosity 
    this.skill = skill 
    this.irresponsibility = irresponsibility 
}
getMark() {
    let mark = (((this.skill+this.curiosity)*(1.5 - this.irresponsability))/(this.laziness*0.25));
   return mark
}

}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

What would you like to happen if the given value is outside the constraints? Should the Intern get a default value?

I would make if statements for every attribute and check if the given value is bigger than the lowest constraint and smaller than the highest:

class Intern {
constructor(name, surname, laziness, curiosity, skill, irresponsibility) {
    this.name = name
    this.surname = surname
if(laziness<=50 && laziness>=20){
    this.laziness = laziness
}
if(curiosity<=100 && curiosity>=0){
    this.curiosity = curiosity 
    this.skill = skill 
    this.irresponsibility = irresponsibility 
}
getMark() {
    let mark = (((this.skill+this.curiosity)*(1.5 - this.irresponsability))/(this.laziness*0.25));
   return mark
}

the <= and >= operator checks if the value is smaller than or equal to or bigger than or equal to.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda