比较一个字符串是否匹配,然后将var设置为double javascript

时间:2016-12-30 03:57:39

标签: javascript

Check the image here

我有一个像这样的输入表,我想输入字符串给Nilai。即我输入字符串" A"对于Nilai,它必须读作5.即A = 5,B = 4和C = 3.

在上图中,SKS为3,Nilai为A,那么N * S必须为15.那么如何将A读为5,B为4,C为3?

我用if语句尝试过,但似乎没有用,或者是否可以在javascript中执行此操作。

这是我的代码



<form name="ips">
<div class="inputan">
<input type="text" value="SKS" disabled="">
<input type="text" value="Nilai" disabled="">
<input type="text" value="N*S" disabled="">
</div>
<div class="inputan">
<input type="text" name="sks1">
<input type="text" name="nilai1"> 
<input type="text" name="ns1" disabled="">
</div>
<div class="inputan">
<input type="text" name="sks2">
<input type="text" name="nilai2">
<input type="text" name="ns2" disabled="">
</div>
<div class="inputan">
<input type="text" name="sks3">
<input type="text" name="nilai3">
<input type="text" name="ns3" disabled="">
</div>
<div class="inputan">
<input type="text" name="sks4">
<input type="text" name="nilai4">
<input type="text" name="ns4" disabled="">
</div>
<div class="inputan">
<input type="text" name="sks5">
<input type="text" name="nilai5">
<input type="text" name="ns5" disabled="">
</div>
<div class="inputan">
<input type="text" name="sks6">
<input type="text" name="nilai6">
<input type="text" name="ns6" disabled="">
</div>
<div class="inputan">
<input type="text" name="totSks" placeholder="Jumlah SKS" disabled="">
<input type="text" disabled="">
<input type="text" name="totNilai" placeholder="IPS" disabled="">
</div>
<input type="button" value="Submit" onClick="hitungIPS()">
<input type="reset" value="Reset">
</form>
&#13;
&#13;
&#13;

我的js

function hitungIPS(){

    var sks1 = parseFloat(document.ips.sks1.value) || 0;
    var sks2 = parseFloat(document.ips.sks2.value) || 0;
    var sks3 = parseFloat(document.ips.sks3.value) || 0;
    var sks4 = parseFloat(document.ips.sks4.value) || 0;
    var sks5 = parseFloat(document.ips.sks5.value) || 0;
    var sks6 = parseFloat(document.ips.sks6.value) || 0;
    var nilai1 = document.ips.nilai1.value || 0;
    var nilai2 = document.ips.nilai2.value || 0;
    var nilai3 = document.ips.nilai3.value || 0;
    var nilai4 = document.ips.nilai4.value || 0;
    var nilai5 = document.ips.nilai5.value || 0;
    var nilai6 = document.ips.nilai6.value || 0;

    var totalSKS = sks1 + sks2 + sks3 + sks4 + sks5 + sks6;
        // nilaiSKS adalah N*S
    var nilaiSKS1 = sks1*n1;
    var nilaiSKS2 = sks2*nilai2;
    var nilaiSKS3 = sks3*nilai3;
    var nilaiSKS4 = sks4*nilai4;
    var nilaiSKS5 = sks5*nilai5;
    var nilaiSKS6 = sks6*nilai6;

    var totalNilai = nilaiSKS1 + nilaiSKS2 + nilaiSKS3 +nilaiSKS4 + nilaiSKS5 + nilaiSKS6;
    var totIPS = totalNilai / totalSKS;

    document.ips.totSks.value = totalSKS;
    document.ips.totNilai.value = totIPS;

    document.ips.ns1.value = nilaiSKS1;
    document.ips.ns2.value = nilaiSKS2;
    document.ips.ns3.value = nilaiSKS3;
    document.ips.ns4.value = nilaiSKS4;
    document.ips.ns5.value = nilaiSKS5;
    document.ips.ns6.value = nilaiSKS6;

    if ( nilai1.match("A") || nilai1.match("a")) {
        var n1 = '5';
        return true;
    }

}

3 个答案:

答案 0 :(得分:0)

function hitungIPS(){

var sks1 = parseFloat(document.ips.sks1.value) || 0;
var sks2 = parseFloat(document.ips.sks2.value) || 0;
var sks3 = parseFloat(document.ips.sks3.value) || 0;
var sks4 = parseFloat(document.ips.sks4.value) || 0;
var sks5 = parseFloat(document.ips.sks5.value) || 0;
var sks6 = parseFloat(document.ips.sks6.value) || 0;
var mappings = {
  'A': 5,
  'B': 4,
  'C': 3
};
var nilai1 = mappings[document.ips.nilai1.value] || 0;
var nilai2 = mappings[document.ips.nilai1.value] || 0;
var nilai3 = mappings[document.ips.nilai1.value] || 0;
var nilai4 = mappings[document.ips.nilai1.value] || 0;
var nilai5 = mappings[document.ips.nilai1.value] || 0;
var nilai6 = mappings[document.ips.nilai1.value] || 0;

var totalSKS = sks1 + sks2 + sks3 + sks4 + sks5 + sks6;
    // nilaiSKS adalah N*S
var nilaiSKS1 = sks1*nilai1;
var nilaiSKS2 = sks2*nilai2;
var nilaiSKS3 = sks3*nilai3;
var nilaiSKS4 = sks4*nilai4;
var nilaiSKS5 = sks5*nilai5;
var nilaiSKS6 = sks6*nilai6;

var totalNilai = nilaiSKS1 + nilaiSKS2 + nilaiSKS3 +nilaiSKS4 + nilaiSKS5 + nilaiSKS6;
var totIPS = totalNilai / totalSKS;

document.ips.totSks.value = totalSKS;
document.ips.totNilai.value = totIPS;

document.ips.ns1.value = nilaiSKS1;
document.ips.ns2.value = nilaiSKS2;
document.ips.ns3.value = nilaiSKS3;
document.ips.ns4.value = nilaiSKS4;
document.ips.ns5.value = nilaiSKS5;
document.ips.ns6.value = nilaiSKS6;
}

http://codepen.io/mr_nameless/pen/LbwXbO
附:如果您在If

之后将其正确放置,var nilai6 = document.ips.nilai1.value || 0;语句也会有效

答案 1 :(得分:0)

您还没有提供实际需求。因此,由于缺乏实际情况,我无法正确理解您的问题。但是,我得出结论,您需要一种能够将A转换为5 B到4和C转换为3的方法。 所以我创建了一个可以解码你的字符串的简单方法我将解释它,这样如果你需要改变它,那么你可以。

    function DECODE(Str_Message) 
    {      
      SIMBOLOS = "ABC";

      Len_Simbolos = SIMBOLOS.length;   
      Len_Str_Message = Str_Message.length; 
      Str_Decoded_Message = "";      
      for (Position = 0; Position < Len_Str_Message; Position++)
      {                    
        Byte_To_Be_Decoded = Str_Message.charAt(Position);     
        Byte_Decoded = 0;          
        for (SecondPosition = 0; SecondPosition < Len_Simbolos;SecondPosition++) 
        {              
            Byte_To_Be_Compared = SIMBOLOS.charAt(SecondPosition);
            if (Byte_To_Be_Decoded == Byte_To_Be_Compared)
            {    
                if(Byte_To_Be_Decoded == SIMBOLOS[SecondPosition])
                {
                    Byte_Decoded = 5-SecondPosition;
                }   
              }          
         }          
         Str_Decoded_Message = Str_Decoded_Message+Byte_Decoded;      
       }      
       return Str_Decoded_Message;  
    } 

这里,如果这个DECODE函数与SIMBOLOS字符串匹配(这里&#34; ABC&#34;),则逐字符解码你的字符串。在那里我应用了两个循环,第一个用于循环存储字符相对于它们的位置,第二个嵌套循环是解码方法的核心,它将所有SIMBOLOS逐个存储并将其与给定字符串进行比较(例如:&#39; A&#39;)如果SIMBOLOS与字符串匹配则它将应用解码技术(此处SIMBOLOS中字符串的位置(&#39; A&#39;)减去5)然后返回值。

以下是此方法的实用方法:

&#13;
&#13;
function DECODE(Str_Message) 
{      
		SIMBOLOS = "ABC";
		
	    Len_Simbolos = SIMBOLOS.length;   
		Len_Str_Message = Str_Message.length; 
        Str_Decoded_Message = "";      
        for (Position = 0; Position < Len_Str_Message; Position++)
		  {                    
            Byte_To_Be_Decoded = Str_Message.charAt(Position);     
            Byte_Decoded = 0;          
            for (SecondPosition = 0; SecondPosition < Len_Simbolos; SecondPosition++) 
			{              
                Byte_To_Be_Compared = SIMBOLOS.charAt(SecondPosition);
                if (Byte_To_Be_Decoded == Byte_To_Be_Compared)
				 {    
					if(Byte_To_Be_Decoded == SIMBOLOS[SecondPosition])
					{
						Byte_Decoded = 5-SecondPosition;
					}	
             	  }          
            }          
			Str_Decoded_Message = Str_Decoded_Message+Byte_Decoded;      
        }      
        return Str_Decoded_Message;  
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<html>
	<head>
		<script>
			$(document).ready(function(){
				$('#decod').on('click',function(){
					var valu = $('#deco').val();
					$("#output").text(DECODE(valu));
				});
			});
		</script>
	</head>
	<body>
		<input type="text" id="deco"></input>
		<button id="decod">decode</button>
		<div id="output"></div>
	</body>
</html>
&#13;
&#13;
&#13;

答案 2 :(得分:-1)

&#13;
&#13;
function hitungIPS(){
    var mappings = {
      A: 5,
      B: 4,
      C: 3
    };
  
    var sks1 = parseInt(document.getElementsByName("sks1")[0].value) || 0;
    var sks2 = parseInt(document.getElementsByName("sks2")[0].value) || 0;
    var sks3 = parseInt(document.getElementsByName("sks3")[0].value) || 0;
    var sks4 = parseInt(document.getElementsByName("sks4")[0].value) || 0;
    var sks5 = parseInt(document.getElementsByName("sks5")[0].value) || 0;
    var sks6 = parseInt(document.getElementsByName("sks6")[0].value) || 0;
    var nilai1 = parseInt(mappings[document.getElementsByName("nilai1")[0].value.toUpperCase()]) || 0;
    var nilai2 = parseInt(mappings[document.getElementsByName("nilai2")[0].value.toUpperCase()]) || 0;
    var nilai3 = parseInt(mappings[document.getElementsByName("nilai3")[0].value.toUpperCase()]) || 0;
    var nilai4 = parseInt(mappings[document.getElementsByName("nilai4")[0].value.toUpperCase()]) || 0;
    var nilai5 = parseInt(mappings[document.getElementsByName("nilai5")[0].value.toUpperCase()]) || 0;
    var nilai6 = parseInt(mappings[document.getElementsByName("nilai6")[0].value.toUpperCase()]) || 0;
    
    
    var nilaiSKS1 = sks1*nilai1;
    var nilaiSKS2 = sks2*nilai2;
    var nilaiSKS3 = sks3*nilai3;
    var nilaiSKS4 = sks4*nilai4;
    var nilaiSKS5 = sks5*nilai5;
    var nilaiSKS6 = sks6*nilai6;

    var totalSKS = sks1 + sks2 + sks3 + sks4 + sks5 + sks6;
    var totalNilaiSks = nilaiSKS1 + nilaiSKS2 + nilaiSKS3 +nilaiSKS4 + nilaiSKS5 + nilaiSKS6;    
    var totalNilai = nilai1 + nilai2 + nilai3 + nilai4 + nilai5 + nilai6;    

    document.getElementsByName("totSks")[0].value = totalSKS;
    document.getElementsByName("totNilai")[0].value = totalNilai;
    document.getElementsByName("totNilaiSks")[0].value = totalNilaiSks;
  
    document.getElementsByName("ns1")[0].value = nilaiSKS1;
    document.getElementsByName("ns2")[0].value = nilaiSKS2;
    document.getElementsByName("ns3")[0].value = nilaiSKS3;
    document.getElementsByName("ns4")[0].value = nilaiSKS4;
    document.getElementsByName("ns5")[0].value = nilaiSKS5;
    document.getElementsByName("ns6")[0].value = nilaiSKS6;
}
&#13;
<form name="ips">
<div class="inputan">
<input type="number" value="SKS" disabled="">
<input type="text" value="Nilai" disabled="">
<input type="text" value="N*S" disabled="">
</div>
<div class="inputan">
<input type="number" name="sks1">
<input type="text" name="nilai1"> 
<input type="text" name="ns1" disabled="">
</div>
<div class="inputan">
<input type="number" name="sks2">
<input type="text" name="nilai2">
<input type="text" name="ns2" disabled="">
</div>
<div class="inputan">
<input type="number" name="sks3">
<input type="text" name="nilai3">
<input type="text" name="ns3" disabled="">
</div>
<div class="inputan">
<input type="number" name="sks4">
<input type="text" name="nilai4">
<input type="text" name="ns4" disabled="">
</div>
<div class="inputan">
<input type="number" name="sks5">
<input type="text" name="nilai5">
<input type="text" name="ns5" disabled="">
</div>
<div class="inputan">
<input type="number" name="sks6">
<input type="text" name="nilai6">
<input type="text" name="ns6" disabled="">
</div>
<div class="inputan">
<input type="text" name="totSks" placeholder="Jumlah SKS" disabled="">
<input type="text" name="totNilai" disabled="">
<input type="text" name="totNilaiSks" placeholder="IPS" disabled="">
</div>
<input type="button" value="Submit" onClick="hitungIPS()">
<input type="reset" value="Reset">
</form>
&#13;
&#13;
&#13;