ccccourse / wp109b

網頁設計 -- 109 下學期 (陳鍾誠課程的學生習題與報告)
1 stars 58 forks source link

習題 2 -- JavaScript 練習 #4

Open ccckmit opened 3 years ago

ccckmit commented 3 years ago
  1. 請寫一個程式可以印出指定數量的 * 號

例如: star(5) 會印出 5 個 * 號

star(5)

*
*
*
*
*
  1. 請寫一個函數可以印出 a..b 之間的所有整數

例如: between(3,8) 會印出

3
4
5
6
7
8

加分題

3 請寫一個函數 primeBetween(a,b) 可以印出 a..b 之間的質數

例如 primeBetween(3,15)

3
5
7
11
13

記得善用函數,先寫一個判斷是否為質數的函數 isPrime(n) 去判斷會比較好

ccckmit commented 3 years ago

陳鍾誠繳交範例

檔案 : star.js

function star(n) {
  for (var i=0; i<n; i++)
    console.log('*')
}

star(5)

執行結果

(base) csienqu-teacher:02 csienqu$ deno run star.js
*
*
*
*
*
ccckmit commented 3 years ago

繳交範例 2

https://github.com/ccckmit/wp109b/tree/main/homework/02

DE45A commented 3 years ago
function star(n){
    for(var i=0;i<n;++i)console.log("*")
}
function between(l,r){
    for(var i=l;i<=r;++i)console.log(i)
}
star(5)
between(3,8)

執行結果

*
*
*
*
*
3
4
5
6
7
8
OohelloworldoO commented 3 years ago

檔案star.js 作業1

function star(n)
{
    for(var i=0;i<n;i++)
    console.log('*')
}
star(5)

執行結果

PS C:\課程\wp109b\deno\homework> deno run homework.js
*
*
*
*
*

作業2

function star(n)
{
    for(var i=3;i<=8;i++)
    console.log(i)
}
star()

執行結果

PS C:\課程\wp109b\deno\homework> deno run homework.js
3
4
5
6
7
8
Kenttsai1 commented 3 years ago

110910509 蔡宗霖

檔案 hw2.js

function star(n) {
    for (var i=0; i<n; i++)
    console.log('*')
}
function between(x,y){
for (var i=x; i<=y; i++ )
     console.log(i)
}

star(5)
between(3,8)

執行結果

KentdeAir:02 kenttsai$ deno run hw2.js
*
*
*
*
*
3
4
5
6
7
8

檔案 hw2p.js


function isprime(n){
    for (var i=2; i<n;i++){
    if(n%i==0){
        return false;
    }
};
    return true;
}
function prime(x,y){
    for (var j=x; j<y;j++)
    if(isprime(j)){
        console.log(j)
    }
}
prime(3,15)

執行結果

KentdeAir:02 kenttsai$ deno run hw2p.js
3
5
7
11
13
Bernie3852 commented 3 years ago

檔案一 : star.js

function star(n)
{
    var stars = []
    for (var i = 0; i != n; ++i)
    {
        stars.push("*");
    }
    console.log(stars);
}
star(7);

執行結果 :

[
  "*", "*", "*",
  "*", "*", "*",
  "*"
]

檔案二 : between.js

function between(a, b)
{
    var numbers = [];
    for (var i = a; i != b; ++i)
    {
        numbers.push(i);
    }
    console.log(numbers);
}
between(9, 29);

執行結果 :

[
   9, 10, 11, 12, 13, 14, 15,
  16, 17, 18, 19, 20, 21, 22,
  23, 24, 25, 26, 27, 28
]

加分題檔案三 : primebetween.js

function isPrime(n)
{
    for (var i = 2; i < n ** 0.5; ++i)
    {
        if (n % i == 0)
        {
            return 0;
        }
    }
    return 1;
}

function primebetween(a, b)
{
    var primes = [];
    for (var i = a; i != b; ++i)
    {
        if (isPrime(i))
            primes.push(i);
    }
    console.log(primes);
}
primebetween(7, 67);

執行結果 :

[
   7,  9, 11, 13, 17, 19, 23,
  25, 29, 31, 37, 41, 43, 47,
  49, 53, 59, 61
]
DE45A commented 3 years ago
var p=new Array(100039)
var v=new Array(0)
function miku(n){
    for(var i=2;i<n;++i){
        if(p[i]==1)v.push(i);
        for(j of v){
            if(i*j>n)break
            p[i*j]=0
            if(i%j==0)break
        }
    }
}
p[0]=p[1]=0;
for(var i=2;i<100039;++i)p[i]=1;
miku(100000)
var l=3,r=15
for(var i=l;i<=r;++i){
    if(p[i]==1)console.log(i)
}

執行結果

3
5
7
11
13
yucing commented 3 years ago

蘇郁晴 https://github.com/yucing/wp109b/tree/main/homework2

YangTiChu commented 3 years ago

楊堤筑 資工一 110910527

https://github.com/YangTiChu/wp109b/tree/main/Homework/20210416

cynthia1231 commented 3 years ago

110810527 張芷榕 作業1 檔案: star.js

function star(n){
    for(var i=0;i<n;i++)
    console.log('*')
}

star(5)

輸出結果:

*
*
*
*
*

作業2 檔案: between.js

function between(a,b){
    for(var i=a;i<=b;i++){
        console.log(i)
    }
}

between(3,8)

輸出結果:

3
4
5
6
7
8

加分題 檔案: primebetween.js

function isPrime(n){
    if(n==1){
        return 1;
    }else{
        for(var i=2;i<n;i++){
            if(n%i==0)
            return 0;
        }
    }
    return 1;

}

function primeBetween(a,b){
    for(var i=a;i<=b;i++){
        if(isPrime(i)){
            console.log(i);
        }
    }
}

primeBetween(3,15)

輸出結果:

3
5
7
11
13
coyk6m3 commented 3 years ago

https://github.com/coyk6m3/wp109b/tree/main/week8

WengChihTung commented 3 years ago

翁志彤 https://github.com/WengChihTung/wp109b/tree/main/homework/02

習題2

  1. 請寫一個程式可以印出指定數量的 號 例如: star(5) 會印出 5 個

star(5)

*
*
*
*
*
  1. 請寫一個函數可以印出 a..b 之間的所有整數 例如: between(3,8) 會印出
3  
4  
5  
6  
7  
8  

加分題

3 請寫一個函數 primeBetween(a,b) 可以印出 a..b 之間的質數

例如 primeBetween(3,15)

3
5
7
11
13

記得善用函數,先寫一個判斷是否為質數的函數 isPrime(n) 去判斷會比較好

第一題

檔案:star5.js

function star(x) {
    for (var i=1;i<=x;i++)
        console.log("*")
}

star(5)

執行結果

wengchihtung@MacBook-Pro 02 % deno run star5.js
*
*
*
*
*

第二題

檔案:between.js

function between(x,y) {
    for (var i=x; i<=y; i++)
        console.log(i)
}

between(3,8)

執行結果:

wengchihtung@MacBook-Pro 02 % deno run between.js
3
4
5
6
7
8

加分題

檔案:pB.js

function isPrime(n) {
    if (n < 2) 
        return 0;
    for (var k=2; k * k <= n; k++)
        if (n % k == 0) 
            return 0;
    return 1;
}

function primeBetween(a, b) {
    for (var i=a; i<=b; i++)
        if (isPrime(i))
        console.log(i);
}

primeBetween(3, 15)

執行結果:

wengchihtung@MacBook-Pro 02 % deno run pB.js
3
5
7
11
13
stayjay commented 3 years ago

function star(n){ for(var i=0;i<n;++i)console.log("*") } function between(a,r){ for(var i=a;i<=r;++i)console.log(i) } star(5) between(3,8)

執行解果 * 3 4 5 6 7 8

ray9016 commented 3 years ago

110910521 https://github.com/ray9016/wp109b/tree/main/Homework/02

Sakura01210 commented 3 years ago

function star(n){ for(var i=0;i<n;++i)console.log("*") } function between(a,r){ for(var i=a;i<=r;++i)console.log(i) } star(5) between(3,8)

執行解果 3 4 5 6 7 8 加分題 function isPrime(n) { if (n < 2) return 0; for (var k=2; k k <= n; k++) if (n % k == 0) return 0; return 1; }

function primeBetween(a, b) { for (var i=a; i<=b; i++) if (isPrime(i)) console.log(i); }

primeBetween(3, 15) 執行 PS C:\Users\spch0.vscode\wp109b\homework\02> deno run plas.js 3 5 7 11 13

iwantall2333 commented 3 years ago

資工一 110910540 莊汶娟 https://iwantall2333.github.io/wp109b/homework/week7 https://github.com/iwantall2333/wp109b/tree/main/homework/week7

feliciachou commented 3 years ago

資工一 110910549 周佳蒨

zhengyucen commented 3 years ago

資工二110810548鄭宇岑 https://github.com/zhengyucen/wp109b/blob/main/homework.js https://github.com/zhengyucen/wp109b/blob/main/homework2.js https://github.com/zhengyucen/wp109b/blob/main/homework3.js

Uriel58 commented 3 years ago

資工一林成也 https://uriel58.github.io/wp109b/week6/star.js

YiTing1110 commented 3 years ago

資工一 張貽婷 110910526 https://github.com/YiTing1110/wp109b/blob/main/week8/README.md

kaifeng273 commented 3 years ago

資工一 吳俊億 110910528 https://github.com/kaifeng273/wp109b/blob/main/homework2/homework2.md

Roy-Roo commented 3 years ago

資工一 羅彥翔 110910519 https://github.com/Roy-Roo/wp109b/blob/main/week8/HW2/README.md

shaoan901226 commented 3 years ago

資工一 110910518 黃紹安 檔案:JS homework.js

for(var i=0;i<n;++i)console.log("*")
}
function between(a,r){
for(var i=a;i<=r;++i)console.log(i)
}
star(6)
between(3,12)

執行結果

PS C:\Users\shaoan901226\wp109b\week8\JS homework.js> deno run plas.js
*
*
*
*
*
*
3
4
5
6
7
8
9
10
11
12
jenny126 commented 3 years ago

HW2

weng0418 commented 3 years ago

資工三 110710547 翁瑋泓 HW2

4YCAaaa commented 3 years ago

資工二 110710336 李思慧 https://github.com/4YCAaaa/wp109b/tree/main/JSExercise_Hw

qwezxca123 commented 3 years ago

資工一 110910533 曾旭宏 homework.js 作業一:

function star(a) {
    for(var i=0;i<a;i++) console.log("*")
}
star(5)

結果:

*
*
*
*
*

作業二:

function between(a,b) {
    for(;a<=b;a++) console.log(a)
}
between(3,8)

結果:

3
4
5
6
7
8

加分題:

function primeBetween(a,b) {
    var t=0
    for(;a<=b;a++)
    {
        t=0
        for(var i=2;i<=a/2;i++)
        {
            if(a%i==0)
            {
                t=1
                break
            }
        }
        if(t==0 && a>1) console.log(a)
    }
}
primeBetween(3,15)

結果:

3
5
7
11
13
Lin610313 commented 3 years ago

資工一110910506林庭光 https://github.com/Lin610313/wp109b/tree/main/week8/homework2

brian891005 commented 3 years ago

資工二 姜玉尚 110810546

al2698 commented 3 years ago

資工一 姜呈諭 110910531 檔案 hw2.js

function star(n) {
    for (var i=0; i<n; i++)
    console.log('*')
}
function between(x,y){
for (var i=x; i<=y; i++ )
     console.log(i)
}

star(5)
between(3,8)

執行結果


*
*
*
*
*
3
4
5
6
7
8
mozi5269 commented 3 years ago

資工一 王昱凱 110910532 work1 hw2.js

function star(a) {
    for(var i=0;i<a;i++) console.log("*")
}
star(5)
*
*
*
*
*

資工一 王昱凱 110910532 work2 hw202.js

function int(a,b){
    for(var i=a;i<=b;i++)
    console.log(i)
}

int(3,8)
3
4
5
6
7
8
yumao57 commented 3 years ago

資工一 劉宸羽 110910529 作業HW2

stereomp3 commented 3 years ago

資工一 魏仲彥 110910541 homework*3 程式原始檔

z22756392z commented 3 years ago

資工一 徐伯元 110910542 https://github.com/z22756392z/wp109b/tree/main/js

Mocha777 commented 3 years ago

110910513 林孫全

檔案 home_work2.js

function stars(n) {
    for (var i=0; i<n; i++)
    console.log('*')
}
function between(x,y){
     for (var i=x; i<=y; i++ )
     console.log(i)
}

stars(5)
between(3,8)

執行結果

todd02 todd$ deno run home_work2.js * 3 4 5 6 7 8 檔案 hw2p.js

function isprime(n){
    for (var i=2; i<n;i++){
    if(n%i==0){
        return false;
    }
};
    return true;
}
function prime(x,y){
    for (var j=x; j<y;j++)
    if(isprime(j)){
        console.log(j)
    }
}
prime(3,15)

執行結果

todd02 todd$ deno run hw2p.js 3 5 7 11 13

gakuplusq commented 3 years ago

110910530 黃劭騏

function star(n) {
for (var i=0; i<n; i++)
console.log('*')
}
function between(x,y){
for (var i=x; i<=y; i++ )
console.log(i)
}

star(5)
between(3,8)

執行結果

KentdeAir:02 kenttsai$ deno run hw2.js
*
*
*
*
*
3
4
5
6
7
8
110612321mickey commented 3 years ago

110612321 黃偉齊

/**
 * Print n Stars.
 * @param {number} n
 */
function star(n) {
    while (n--) {
        console.log('*');
    }
}

/**
 * Print integer between min to max.
 * @param {number} min
 * @param {number} max
 */
function between(min, max) {
    for (let i = min; i <= max; i++) {
        console.log(i);
    }
}

/**
 * Check if n is an prime.
 * @param {number} n
 * @return {boolean}
 */
function isPrime(n) {
    if (n < 2) {
        return false;
    }
    for (let i = 2; i <= n / 2; i++) {
        if (n % i === 0) {
            return false;
        }
    }
    return true;
}

/**
 * Print prime between a to b.
 * @param {number} a must < b
 * @param {number} b
 */
function primeBetween(a, b) {
    for (let i = a; i <= b; i++) {
        if (isPrime(i)) {
            console.log(i);
        }
    }
}
cabdy1735 commented 3 years ago

110910534 李翊慈 https://github.com/cabdy1735/wp109b/tree/main/week8

Yongsin0 commented 3 years ago

資工三 110710544 陳詠鑫 HW2

wukunru commented 3 years ago

110910510吳昆儒 檔案 hw2.js

function star(n){
    for(var i=0;i<n;++i)console.log("*")
}

star(5)

執行結果

*
*
*
*
*

檔案 hw2().js

function between(x,y){
for (var i=x; i<=y; i++ )
     console.log(i)
}

between(3,8)

執行結果

3
4
5
6
7
8

檔案 hw2<>.js

function isprime(n){
    for (var i=2; i<n;i++){
    if(n%i==0){
        return false;
    }
};
    return true;
}
function prime(x,y){
    for (var j=x; j<y;j++)
    if(isprime(j)){
        console.log(j)
    }
}
prime(2,9)

執行結果

2
3
5
7
Paouser commented 3 years ago

110910522 劉煒辰 https://github.com/Paouser/wp109b/tree/main/homework/02

檔案:star.js

function star(n) { for (var i=0; i<n ; i++) console.log('*') }

star (5)

執行結果 *

第二題 檔案:between.js

function between (a,b){ for (var i=0;i<=b;i++) if (i>=a) console.log(i);

}

between(2,9)

執行結果

2 3 4 5 6 7 8 9

第三題

檔案:isPrime.js

function between(a,b){ for (var i=0;i<=b;i++) if (i>=a) isPrime(i); }

function isPrime(a){ var b=0; for(var i=1;i<a;i++) if (a%i==0)b++ if (b==1)console.log(a) }

between(2,11)

執行結果

2 3 5 7 11

RyanChen-01 commented 3 years ago

110910515 陳文吉 第一題 檔案:star.js

function star(n) {
    for (var i=0;i<n;i++)
    console.log('*')
}

star(5)

執行結果 PS D:\網頁設計\wp109b\homework\hw02> deno run star.js *

第二題 檔案:between.js

function between(a,b){
        for(var i=a;i<=b;++i)
        console.log(i)
}

between(3,8)

執行結果: PS D:\網頁設計\wp109b\homework\hw02> deno run between.js
3 4 5 6 7 8

加分題 檔案:bonus.js

function between(a,b){
    for(var n=a;n<b;n++)
    if (isPrime(n)){
        console.log(n)
    }
}

function isPrime(n){

    if(n<2)
    {
        return false
    }
    for (var z=2;z<n;z++)
    {
        if(n%z==0){
            return false
        }
        return true
    }
}

between(3,15)

執行結果 PS D:\網頁設計\wp109b\homework\hw02> deno run bonus.js 3 5 7 9 11 13

derek120432 commented 3 years ago

資工一 李韋德 prime:https://github.com/derek120432/wp109b/blob/main/homework/prime.js star:https://github.com/derek120432/wp109b/blob/main/homework/star.js

nakirifumiya commented 3 years ago

資工一 王澤瑋

https://github.com/nakirifumiya/wp109b/blob/main/hw/hw2.md

kmmc2490173 commented 3 years ago

110713334 林鋐彰 檔案:hw2.js function st(n) { for(var i=0;i<n;++i)console.log("*") } function between(a,r){ for(var i=a;i<=r;++i)console.log(i) } star(7) between(5,13) 執行結果

PS C:\Users\kmmc2490173\wp109b\week8\ hw2.js> deno run hw2.js * 5 6 7 8 9 10 11 12 13

choco427 commented 3 years ago

110910524 張詠翔

檔案一 : star.js

function star(n)
{
    var stars = []
    for (var i = 0; i != n; ++i)
    {
        stars.push("*");
    }
    console.log(stars);
}
star(7);

執行結果 :

[ "", "", "", "", "", "", "*" ]

檔案二 : between.js

function between(a, b)
{
    var numbers = [];
    for (var i = a; i != b; ++i)
    {
        numbers.push(i);
    }
    console.log(numbers);
}
between(9, 29);

執行結果 :

[ 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28 ]

加分題 : primebetween.js

function isPrime(n)
{
    for (var i = 2; i < n ** 0.5; ++i)
    {
        if (n % i == 0)
        {
            return 0;
        }
    }
    return 1;
}

function primebetween(a, b)
{
    var primes = [];
    for (var i = a; i != b; ++i)
    {
        if (isPrime(i))
            primes.push(i);
    }
    console.log(primes);
}
primebetween(7, 29);

執行結果 :

[ 3, 5, 7, 11, 13, 17, 19, 23 ,29 ]

QuiLinxinag commented 3 years ago

110910536 邱麟翔 作業https://github.com/QuiLinxinag/wp109b/tree/main/homework

Selesfia commented 3 years ago

資工一 胡禎恩 110910552 HomeWork2

zraiz commented 3 years ago

工管一 110911542 邵南翔

1st star

function star(n)
{
    for(var i=0; i<=n; i++)
    {
        console.log("*");
    }
}
star(5);

OutPut

*
*
*
*
*

2nd between

function between(a,b)
{
    for(var a; a<=b; a++)
    {
        console.log(a);
    }
}
between(3,8);

OutPut

3
4
5
6
7
8

3rd prime between

function isPrime(n){
    if(n<2) return 0
    for(var x=2;x*x<=n;++x) if(n%x == 0) return 0
    return 1
}

function between(a,b)
{
    for(var i=a;i<=b;++i) if(isPrime(i))
    console.log(i)
}
between(15,30)

OutPut

17
19
23
29
po-hsiang666 commented 3 years ago

資工一 110910544 張博翔 程式一

function star(n)
{
    for(var i=0; i<=n; i++)
    {
        console.log("*");
    }
}
star(5);

output

*
*
*
*
*

程式二

function between(a,b)
{
    for(var a; a<=b; a++)
    {
        console.log(a);
    }
}
between(3,8);

output

3
4
5
6
7
8

程式三

function isPrime(n){
    if(n<2) return 0
    for(var x=2;x*x<=n;++x) if(n%x == 0) return 0
    return 1
}

function between(a,b)
{
    for(var i=a;i<=b;++i) if(isPrime(i))
    console.log(i)
}
between(15,30)

output

17
19
23
29