请帮助JavaScript [英] JavaScript help please

查看:83
本文介绍了请帮助JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个JS函数来获取任何大于1的用户输入,并计算从1到用户输入的数字之和。例如,如果用户输入5,则程序应计算从1到5的总和,即15。



我将如何进行此操作?

解决方案

这是你的作业,所以我不会给你代码:但它真的不复杂。



首先,你知道如何创建一个输入和一个按钮,并连接一个测试功能,是吗?

如果没有,那么你需要学习,但是这样做:

 <  !DOCTYPE     html  >  
< html >
< body >

< h1 > 我的第一个JavaScript < / h1 >
< p > 点击按钮显示日期。< / p >
< p id = 演示 > < / p >
< 输入 id = number type = < span class =code-keyword> number value = 1 > < br >
< 按钮 type = 按钮 < span class =code-attribute> onclick = myFunction() > 尝试< / button >

< script >
function myFunction()
{
var x = document .getElementById(' number')。value;
alert(x);
}
< / 脚本 >

< / body >
< / html >



现在,您只需要do是var x = ...行和alert ...行之间的位。



你知道如何循环,是吗?因此,创建一个变量来保存总数,并循环输入的次数,将值添加到总数中,然后将值减1。

简单!


阅读 JavaScript ,显示您的进度。 :)



这里我们回答成员在编程时遇到的具体问题。



我们无法工作在整个源代码上。你需要自己做。



如果你在编码时遇到任何困难,请随时回到这里,并提出另一个问题来描述这个场景。



各位会员很乐意为您提供帮助。 :)

Write a JS function to take any user input which should be larger than 1 and count a sum from 1 to the number that user entered. For example, if a user entered 5, the program should count the sum from 1 to 5 which will be 15.

How would I go about in doing this?

解决方案

It's your homework, so I won't give you code: But it's really not complex.

First, you know how to create an input and a button, and hook up a function for testing, yes?
If not, then you need to learn, but this will do it:

<!DOCTYPE html>
<html>
<body>

<h1>My First JavaScript</h1>
<p>Click the button to display the date.</p>
<p id="demo"></p>
<input id="number" type="number" value="1"><br>
<button type="button" onclick="myFunction()">Try it</button>

<script>
function myFunction()
{
var x = document.getElementById('number').value;
alert(x);
}
</script>

</body>
</html>


Now, all you need to do is the bit between the "var x=..." line and the "alert..." line.

You know how to loop, yes? So create a variable to hold the total, and loop as many times as entered, adding the value to the total, and then reducing the value by one.
Simples!


Read about JavaScript, show your progress. :)

Here we answer specific questions which members face during programming.

We can't work on the whole source code. You need to do that yourself.

If you face any difficulty while coding, feel free to come back here and ask another question with specific issue describing the scenario.

Members will be happy to help you then. :)


这篇关于请帮助JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆