如何使用JavaScript添加,减去或乘以两个变量?

时间:2016-05-21 04:58:45

标签: javascript

<!DOCTYPE html>
<head><title> test ! </title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
</head>
<html>
<body>

<div class="container">

first number : <input class="form-control" id="num1" />
<br>
second number : <input class="form-control" id="num2" />
<br>
<select id="mathtype">
   <option value="add"> addition </option>
   <option value="sub"> subtraction </option>
   <option value="mul"> multiplication </option>
</select>
<br><br>
<button class="btn btn-primary" onclick="submit()" > submit </button>
<br><br>
<input type="text" id="output" >

</div>



</body>
<script src="js/jquery-1.9.1.js"></script>
<script>

function submit(){

    var mathtype = document.getElementById['mathtype'];

    if (mathtype == add ) {
        return num1 + num2;
    } else if (mathtype == sub ) {
        return num1 - num2;
    } else if (mathtype == mul ) {
        return num1 * num2;
    }   

    return true;

}

</script>

</html>

我当前的错误:

  

SyntaxError:函数语句需要名称。

我想创建一个程序,根据所选的值(add,sub,mul)执行数学运算,点击提交后,输入中的答案显示为id“output”。< / p>

1 个答案:

答案 0 :(得分:1)

import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import minimize

def initial_trend(series, slen):
  sum = 0.0
  for i in range(slen):
    sum += float(series[i+slen] - series[i]) / slen
  return sum / slen

def initial_seasonal_components(series, slen):
  seasonals = {}
  season_averages = []
  n_seasons = int(len(series)/slen)
  # compute season averages
  for j in range(n_seasons):
    season_averages.append(sum(series[slen*j:slen*j+slen])/float(slen))
  # compute initial values
  for i in range(slen):
    sum_of_vals_over_avg = 0.0
    for j in range(n_seasons):
      sum_of_vals_over_avg += series[slen*j+i]-season_averages[j]
    seasonals[i] = sum_of_vals_over_avg/n_seasons
  return seasonals

def triple_exponential_smoothing(series, slen, alpha, beta, gamma, n_preds):
  result = []
  seasonals = initial_seasonal_components(series, slen)
  for i in range(len(series)+n_preds):
    if i == 0: # initial values
      smooth = series[0]
      trend = initial_trend(series, slen)
      result.append(series[0])
      continue
    if i >= len(series): # we are forecasting
      m = len(series) - i + 1
      result.append((smooth + m*trend) + seasonals[i%slen])
    else:
      val = series[i]
      last_smooth, smooth = smooth, alpha*(val-seasonals[i%slen]) + (1-alpha)*(smooth+trend)
      trend = beta * (smooth-last_smooth) + (1-beta)*trend
      seasonals[i%slen] = gamma*(val-smooth) + (1-gamma)*seasonals[i%slen]
      result.append(smooth+trend+seasonals[i%slen])
  return np.array(result)

def sse(alpha, beta, gamma):
  pred = triple_exponential_smoothing(series, 12, alpha, beta, gamma, 24)
  return sum((pred[:series.shape[0]] - series)**2)

series = np.array([30,21,29,31,40,48,53,47,37,39,31,29,17,9,20,24,27,35,41,38,
          27,31,27,26,21,13,21,18,33,35,40,36,22,24,21,20,17,14,17,19,
          26,29,40,31,20,24,18,26,17,9,17,21,28,32,46,33,23,28,22,27,
          18,8,17,21,31,34,44,38,31,30,26,32])

#pred = triple_exponential_smoothing(series, 12, 0.716, 0.029, 0.993, 24)
pred = triple_exponential_smoothing(series, 12, 0.1, 0.9, 0.1, 24)

fig, ax = plt.subplots(1,1)
ax.plot(pred, 'r:', label='Forecast')
ax.plot(series, 'k-', label='Actual', lw=1)
ax.legend(loc='best')
plt.show()
function submit() {
  var mathtype = document.getElementById('mathtype').value;
  //Get the value of the select element..Correct the typo([])
  var num1 = Number(document.getElementById('num1').value);
  //Get the value of `num1` input and convert it to type `Number`
  var num2 = Number(document.getElementById('num2').value);
  //Get the value of `num2` input and convert it to type `Number`
  if (mathtype == 'add') {
    //Compare with `string` as value of select input will be of type string, you did not have variable `add` to be tested
    document.getElementById('output').value = num1 + num2;
  } else if (mathtype == 'sub') {
    //Compare with `string` as value of select input will be of type string, you did not have variable `sub` to be tested
    document.getElementById('output').value = num1 - num2;
  } else if (mathtype == 'mul') {
    //Compare with `string` as value of select input will be of type string, you did not have variable `mul` to be tested
    document.getElementById('output').value = num1 * num2;
  }
}

注意 :浏览评论以获取详细说明