Exercise 1: Calculate the number of integers between 1 and 100;
var sum = 0;
for (var i =1; i<=100; i++) {
Sum+=i;
}
Console.log (SUM)
Exercise 2: Require the user to enter a user name and password, as long as not admin, 888888 has been prompted the user name or password error, please re-enter.
var name = +prompt ("Enter user name");
var password = +prompt ("Enter password");
while (name! = "Admin" && password! = "888888") {
Prompt ("username or password is wrong, please re-enter")
}
Exercise 3: Constantly ask the user to enter the student's name and enter the end of Q.
var name = prompt ("Enter student name");
while (name! = "Q") {
Prompt ("Enter student name")
}
Exercise 4: Constantly ask the user to enter a number, and then print the number twice times, when the user enters Q when the program quits.
var sum = prompt ("Enter a number");
do{
var a = 2 *sum
Console.log (a)
Break
}while (sum! = "Q") {
Prompt ("Enter a number")
}
Exercise 5: Constantly asking the user to enter a number (assuming that the user is entering a positive integer), and when the user enters end, displays the maximum value in the number just entered
var num = prompt ("Enter a positive integer");
var big = num;
while (true) {
var num1 = prompt ("Enter a positive integer again");
if (num1== "end") {
Alert (BIG)
Break
}else if (num1>big) {
BIG=NUM1;
}
}
1 to 1-100 and the average
var sum = 0;
for (var i=1; i<101; i++) {
sum+= i;
}
Console.log (sum);
Console.log (sum/100);
2 for all even numbers between 1-100 and
var sum = 0;
for (var i=1; i<=100; i++) {
if (i%2==0) {
sum+= i;
}
}
Console.log (sum);
3 for 1-100 all odd and
var sum = 0;
for (var i=1; i<=100; i++) {
if (i%2!=0) {
sum+= i;
}
}
Console.log (sum);
4 of the principal 10000 yuan deposited in the bank, the annual interest rate is 3 per thousand, every 1 years, the principal and interest added as the new principal. After 5 years of calculation, what is the amount of the principal obtained?
var money = 10000;
for (var i=1; i<=5; i++) {
money+= money* (3/1000);
}
Console.log (money-10000)
5 cycle into the age of 10 people and calculate the average age, if the input data appears negative or greater than 100, immediately stop the input and error, the previous age sum and average print out
var arr = [];
var sum = 0;
for (i=1;i<=10; i++) {
Arr[i]=parseint (Prompt ("Please enter Age")
}
6 in while with the break implementation requires the user to always enter the user name and password, as long as not admin, 88888 has been prompted to re-enter, if correct, the login success.
while (true) {
var name = parseint (Prompt ("Please enter user name")
var password = parseint (Prompt ("Please enter password")
if (name!= "admin" && password = = "88888") {
Console.log ("Login Successful")
Break
}
}
7 the integers between 1~100 to get the current number with a cumulative value greater than 20 (e.g. 1+2+3+4+5+6=21) results 6 sum>=20
var sum = 0;
for (var i=1; i<=100; i++) {
sum+= i;
if (sum>20) {
Console.log (i);
Break
}
}
8 Use while continue to calculate the addition of 1 to 100 (inclusive) except for all integers that can be divisible by 7.
var sum = 0;
var i = 1;
while (i<=100) {
if (i% 7! = 0) {
sum+= i;
i++;
}else{
i++;
Continue
}
}
Console.log (SUM)
9 Find all the primes in 100 (prime is prime, can only be divisible by 1 and itself) and the number of primes
var count = 0;
for (Var i=2;i<=100;i++) {
var B = true;
for (var j= 2;j<i;j++) {
if (i%j==0) {
B=false;
Break
}
}
if (b) {
count++;
Console.log (i+ "= =" +count)
}
}
10 the number of a set of numbers and
var arr = [12,23,34,45,56,67,78,89,90];
var sum = 0;
for (var i =0; i<arr.length;i++) {
sum+= Arr[i];
}
Console.log (SUM)
11 finding the maximum of a set of numbers
var arr = [12,23,34,45,56,67,78,89,90];
var Max =arr[0];
for (var i= 1;i<arr.length;i++) {
if (Max<arr[i]) {
max = Arr[i];
}
}
Console.log (max);
12 Remove the 0 items in the array, save the value of not 0 to a new array, and create a new array
var arr = [12,23,0,34,45,0,56,67,0,78,89,90];
var newarr=[];
var j=0;
for (Var i=0;i<arr.length;i++) {
if (arr[i]!=0) {
Newarr[j]=arr[i];
j + +;
}
}
Console.log (NEWARR);
13 bubble sort, from small to large [98,67,58,97,4,53,62,30]
var arr = [98,67,58,97,4,53,62,30];
for (Var i=0;i<arr.length-1;i++) {
for (Var j=0;j<arr.length-1-i;j++) {
if (Arr[j]>arr[j+1]) {
var temp =arr[j];
ARR[J]=ARR[J+1];
Arr[j+1]=temp;
}
}
}
Console.log (arr);
JavaScript Beginner Little Exercise