Javascript conditions not working

Asked

Viewed 88 times

1

I am making a calculation with the following information:

Style, experience, height and weight.

With height and weight, I take the user’s BMI and compare it to their experience. With style, I define the type of board.

First, I take the board style. With the board style "in hand", I compare the BMI and the user experience and display the result.

The problem is, only the "SURF" option is working. When I choose the "Standup Paddle" option, it doesn’t work.

How to solve this?

function calcularIMC(){  
var estilo = document.getElementById("estilo").value; 
var experiencia = document.getElementById("experiencia").value; 
var altura = document.getElementById("altura").value; 
var peso = document.getElementById("peso").value; 
var imc = 0;  

 
   if(altura >0 && peso >0){
     imc = peso / (altura * altura);
    }
  
  
    if(estilo == "StandUp Paddle"){  

      if((experiencia>=3) && (imc<=29)){        
        alert("SUP SURF");                 
      } 
       else if((experiencia<=3)&&(imc>29)){  
          alert("FUNBOARD");                
        }
                 
     }
      else if(estilo == "Surf"){            
         if(imc<=25){          
          alert("PRANCHINHA");  
      }
      else{         
          alert("LONGBOARD");        
          }
     }
}
<span>Estilo</span><br>
<select name="estilo" id="estilo" value="estilo" required>
   <option disabled selected hidden>Escolha seu estilo</option>
   <option>StandUp Paddle</option>
   <option>Surf</option>
</select><br><br>

<span>Experiência</span><br>
  <select name="experiencia" id="experiencia" required>
  <option disabled selected hidden>Escolha sua experiência</option>
  <option value="0">sem experiência</option>
  <option value="1">menos de 1 mês</option>
  <option value="2">até 3 meses</option>
  <option value="3">até 6 meses</option>
  <option value="4">1 ano ou mais</option>
</select>  <br> <br>


<span>Altura</span><br>
<select name="altura" id="altura" required>
  <option disabled selected hidden>Escolha sua altura</option>
  <option>1.60</option>
  <option>1.61</option>
  <option>1.62</option>
  <option>1.63</option>
  <option>1.64</option>
  <option>1.65</option>
  <option>1.66</option>
  <option>1.67</option>
  <option>1.68</option>
  <option>1.69</option>
  <option>1.70</option>
  <option>1.71</option>
  <option>1.72</option>
  <option>1.73</option>
  <option>1.74</option>
  <option>1.75</option>
  <option>1.76</option>
  <option>1.77</option>
  <option>1.78</option>
  <option>1.79</option>
  <option>1.80</option>
  <option>1.81</option>
  <option>1.82</option>
  <option>1.83</option>
  <option>1.84</option>
  <option>1.85</option>
  <option>1.86</option>
  <option>1.87</option>
  <option>1.88</option>
  <option>1.89</option>
  <option>1.90</option>
  <option>1.91</option>
  <option>1.92</option>
  <option>1.93</option>
  <option>1.94</option>
  <option>1.95</option>
  <option>1.96</option>
  <option>1.97</option>
  <option>1.98</option>
  <option>1.99</option>
  <option>2.00</option> 
</select><br><br>


<span>Peso</span><br>
<select name="peso" id="peso" required>
  <option disabled selected hidden>Escolha seu peso</option>
  <option>50</option>
  <option>51</option>
  <option>52</option>
  <option>53</option>        
  <option>54</option>
  <option>55</option>
  <option>56</option>
  <option>57</option>
  <option>58</option>
  <option>59</option>
  <option>60</option>
  <option>61</option>
  <option>62</option>
  <option>63</option>
  <option>64</option>
  <option>65</option>
  <option>66</option>
  <option>67</option>
  <option>68</option>
  <option>69</option>
  <option>70</option>
  <option>71</option>
  <option>72</option>
  <option>73</option>
  <option>74</option>
  <option>75</option>
  <option>76</option>
  <option>77</option>
  <option>78</option>
  <option>79</option>
  <option>80</option>
  <option>81</option>
  <option>82</option>
  <option>83</option>
  <option>84</option>
  <option>85</option>
  <option>86</option>
  <option>87</option>
  <option>88</option>
  <option>89</option>
  <option>90</option>
  <option>91</option>
  <option>92</option>
  <option>93</option>
  <option>94</option>
  <option>95</option>
  <option>96</option>
  <option>97</option>
  <option>98</option>
  <option>99</option>
  <option>100</option>
</select><br><br>

    
 <button id="Enviar" onclick="calcularIMC();">Send</button>

I got through this way:

if(estilo == "Surf"){            
   if((imc<=25) && (resultado == 5)){          
    alert("PRANCHINHA");  
}
else{         
    alert("LONGBOARD");        
    }
}    
else if(estilo == "SUP"){      
  if((experiencia >= 3) && (imc <=29)){
    alert("Sup Surf");
  } else{
    alert("FUNBOARD")
  }                       
}
  • 1

    I don’t understand what the problem is.

  • Run the Snippet by choosing the "Standup Paddle style"

  • 1

    I still don’t understand what the problem is. If I don’t explain, I’ll continue not knowing.

  • @bigown I updated the question. Take a look pf

  • 1

    It doesn’t work is very generic, you have to explain what should happen.

  • 1

    Within the if(estilo == "StandUp Paddle") you just forehead experiencia>=3 (up to 6 months). And if the user chooses an experience less than 3?

  • It’s working for me. I just don’t know if it’s the way you want it, we can’t guess the way you want it.

  • @abfurlan Truth. I reversed the symbol.

  • @Bigown When I choose the option StandUp Paddle Alert does not fire. This is the problem

  • I answered what I could, otherwise I’m waiting for an explanation of what the code should do. Repeating the same thing without explaining what I should do.

  • The purpose of the code is to define an ideal board type for the user. According to your information (board style, experience time, height and weight). With his height and weight, I calculate the BMI - Body Mass Index, so I have a sense of the health status of the user. With this information "in hand", I compare with the experience time and style of plank and give the result

  • I voted to close because the last issue made it clear that the question cannot be answered by anyone but the author himself. Even the answer put does not work correctly in any definition unless it returns to have behavior similar to what the author says is a problem, IE, there are situations where nothing is done.

  • @Luizsantos Did the answer solve your problem? Do you think you can accept it? If you don’t know how you do it, see [tour]. This would help a lot to indicate that the solution was useful to you and to give an indication that there was a satisfactory solution. You can also vote on any question or answer you find useful on the entire site.

Show 8 more comments

1 answer

2

This is a way that could solve the problem, but I cannot guarantee because the question does not explain what is the desired result. There was even an issue that completely changed what it was supposed to do. Everything suggests that the editing has made the code even more wrong, but there is no guarantee. Then there was another issue putting a solution that is certainly wrong and even if it was the right one we would still conclude that only the AP could answer the question.

Note that organizing code helps you understand it.

function calcularIMC() {  
    var estilo = document.getElementById("estilo").value; 
    var experiencia = document.getElementById("experiencia").value; 
    var altura = document.getElementById("altura").value; 
    var peso = document.getElementById("peso").value; 
    var imc = 0;  
    if (altura > 0 && peso > 0) {
        imc = peso / (altura * altura);
    }
    if (estilo == "StandUp Paddle") {  
        if (experiencia >=3) {   
            if (imc<=29) {   
                alert("SUP SURF");                 
            } else { 
                alert("FUNBOARD");                
            }
        } else { 
            alert("Alguma mensagem pra quem não tem experiência suficiente");
        }
    } else if (estilo == "Surf") {            
         if (imc <= 25) {          
             alert("PRANCHINHA");  
         } else {         
             alert("LONGBOARD");        
         }
    }
}
<span>Estilo</span><br>
<select name="estilo" id="estilo" value="estilo" required>
   <option disabled selected hidden>Escolha seu estilo</option>
   <option>StandUp Paddle</option>
   <option>Surf</option>
</select><br><br>

<span>Experiência</span><br>
  <select name="experiencia" id="experiencia" required>
  <option disabled selected hidden>Escolha sua experiência</option>
  <option value="0">sem experiência</option>
  <option value="1">menos de 1 mês</option>
  <option value="2">até 3 meses</option>
  <option value="3">até 6 meses</option>
  <option value="4">1 ano ou mais</option>
</select>  <br> <br>


<span>Altura</span><br>
<select name="altura" id="altura" required>
  <option disabled selected hidden>Escolha sua altura</option>
  <option>1.60</option>
  <option>1.61</option>
  <option>1.62</option>
  <option>1.63</option>
  <option>1.64</option>
  <option>1.65</option>
  <option>1.66</option>
  <option>1.67</option>
  <option>1.68</option>
  <option>1.69</option>
  <option>1.70</option>
  <option>1.71</option>
  <option>1.72</option>
  <option>1.73</option>
  <option>1.74</option>
  <option>1.75</option>
  <option>1.76</option>
  <option>1.77</option>
  <option>1.78</option>
  <option>1.79</option>
  <option>1.80</option>
  <option>1.81</option>
  <option>1.82</option>
  <option>1.83</option>
  <option>1.84</option>
  <option>1.85</option>
  <option>1.86</option>
  <option>1.87</option>
  <option>1.88</option>
  <option>1.89</option>
  <option>1.90</option>
  <option>1.91</option>
  <option>1.92</option>
  <option>1.93</option>
  <option>1.94</option>
  <option>1.95</option>
  <option>1.96</option>
  <option>1.97</option>
  <option>1.98</option>
  <option>1.99</option>
  <option>2.00</option> 
</select><br><br>


<span>Peso</span><br>
<select name="peso" id="peso" required>
  <option disabled selected hidden>Escolha seu peso</option>
  <option>50</option>
  <option>51</option>
  <option>52</option>
  <option>53</option>        
  <option>54</option>
  <option>55</option>
  <option>56</option>
  <option>57</option>
  <option>58</option>
  <option>59</option>
  <option>60</option>
  <option>61</option>
  <option>62</option>
  <option>63</option>
  <option>64</option>
  <option>65</option>
  <option>66</option>
  <option>67</option>
  <option>68</option>
  <option>69</option>
  <option>70</option>
  <option>71</option>
  <option>72</option>
  <option>73</option>
  <option>74</option>
  <option>75</option>
  <option>76</option>
  <option>77</option>
  <option>78</option>
  <option>79</option>
  <option>80</option>
  <option>81</option>
  <option>82</option>
  <option>83</option>
  <option>84</option>
  <option>85</option>
  <option>86</option>
  <option>87</option>
  <option>88</option>
  <option>89</option>
  <option>90</option>
  <option>91</option>
  <option>92</option>
  <option>93</option>
  <option>94</option>
  <option>95</option>
  <option>96</option>
  <option>97</option>
  <option>98</option>
  <option>99</option>
  <option>100</option>
</select><br><br>

    
 <button id="Enviar" onclick="calcularIMC();">Send</button>

I put in the Github for future reference.

  • Incus of these three if. I couldn’t do it in a direct way if((experiencia>= 3) && (imc<= 29)) ?

  • It depends, you have not yet said what you want. Have case that can, have case that can not.

  • The purpose of the code is to define an ideal board type for the user. According to your information (board style, experience time, height and weight). With his height and weight, I calculate the BMI - Body Mass Index, so I have a sense of the health status of the user. With this information "in hand", I compare with the experience time and style of plank and give the result

  • SE the style of plank for STANDUP PADDLE... To experiencia is greater than or equal to 6 meses and his BMI is <=29, `Alert("SUP SURF");

  • I’m doing this in a direct way if((experiencia>= 3) && (imc<= 29)) and it’s not taking. This is my problem

  • I can’t help more than that without giving more details. You can test the answer and see that it works. If it doesn’t work the way you want it, I don’t have anything to do, you don’t explain it in a way that helps. Your problem is probably not code, it’s not knowing what you want to do, and then no one can help you.

  • I did it differently, but I got what I wanted. Thanks for the willingness to help! I put the code in the question.

Show 2 more comments

Browser other questions tagged

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