repl.it
@mcuringa/

for-loop-exercises

ES6

No description

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// these function do not have return statements and cannot be easily tested with assert
// ====================================================================================

// 1. Write a function that prints `We like Javascript!` 1000 times. 



// 2. Write a function `printOdds(start, end)` which prints all of the odd number starting with `start`
//     up to and including `end`.


// 3. Write a function `countByTens` that counts to 10,000 by 10s (printing the sequence 10, 20, 30, .. 10,000).



// these functions must return a value. use the test functions given to test your code
// ====================================================================================



// 4. **Biggest prime.** Write a function `biggestPrime(a, b)` which returns the
//    largest prime number between integers `a` and `b` (inclusive). If either `a` or `b` are negative,
//    you should return the special Javascript value `NaN` (not-a-number) to indicate
//    that the function received illegal parameters. If no prime numbers  are found in the range,
//    return 0. You can use isPrime as defined here.

function isPrime (n) {

  if (n < 2) {
    return false;
  }
  for (let i = 2; i < n; i++) {
    if (n % i === 0) {
      return false;
    }
  }

  return true;
}


function biggestPrime(a, b) {
  return 0;
}

function test_biggestPrime() {
  console.assert(isNaN(biggestPrime(-2, 5)), "negatives should produce NaN");
  console.assert(biggestPrime(8, 10) === 0, "there are no primes in [8, 9, 10]");
  console.assert(biggestPrime(42000, 42073) === 42073, "42073 is prime, should be found");
  console.assert(biggestPrime(0, 100) === 97, "97 is the largest prime less than 100");

}
test_biggestPrime();


// 5. (hard) **Center text.** The `.length` attributes of a string tells us how
//    many characters are in the string, `"cat".length === 3`. Using this property
//    and a `for` loop, write a function that centers text in the console. Your function
//    must have two parameters -- the text that needs to be centered, and the number
//    of characters in the console. The function returns a new string, padded with
//    enough blank spaces on the left side so that it will be centered. If the text
//    can't be perfectly centered, it should be one space to the left. Do not
//    pad your return string with spaces on the right.
//    _The function does not print to the console_.


function centerText(txt, cols) {
  return "";
}

function demoCenteredText() {
  console.log(centerText("Intro to Computer Programming", 60));
  console.log(centerText("Javascript", 60));
  console.log(centerText("Fall 2019", 60));
}

// demoCenteredText();

function test_centerText() {
  console.assert(centerText("a", 3) === " a", "failed to center  'a', 3");
  console.assert(centerText("abc", 10) === "   abc", "failed to center  'abc', 10");
  console.assert(centerText("ab cd", 20) === "       ab cd", "failed to center  'ab cd', 20");
}


// 6. (bonus) Consider the `isPrime` example above. Our solution would be considered
//    a **naive** solution -- a solution which solve the problem in a basic, but
//    not most efficient or _elegant_ manner. Thinking (or reading) about prime
//    numbers, can you **refactor** `isPrime` so that it can determine if a number
//    is prime without having to complete all of the iterations between 2 and `n`? 


function test_isPrime() {
  console.assert(isPrime(0) === false, "zero is not prime");
  console.assert(isPrime(1) === false, "1 is not prime");
  console.assert(isPrime(2) === true, "2 is prime");
  console.assert(isPrime(7) === true, "7 is prime");
  console.assert(isPrime(100) === false, "100 is not prime");
  // some know primes via:
  // https://primes.utm.edu/lists/small/10000.txt
  console.assert(isPrime(42073) === true, "42073 is prime");
  console.assert(isPrime(104651) === true, "104651 is prime");
  console.assert(isPrime(104653) === false, "104653 is not prime");
}
test_isPrime()



// 7. (bonus) Write a function `countPrimes(a, b)` that returns the
// number of primes found between `a` and `b`. Include `a` and `b` in the count if they are primes.

function countPrimes(a, b) {
  return 0;
}

function test_countPrimes() {
  console.assert(countPrimes(2, 5) === 3, "there are 3 primes in [2..5]");
  console.assert(countPrimes(8, 10) === 0, "there are 0 primes in [8..10]");
  // 99989  99991 100003 100019 100043 100049 100057 100069 100103 100109
  console.assert(countPrimes(99989, 100108) === 9, "there are 9 primes in [99989..100108]");

}
Babel Compiler v6.4.4 Copyright (c) 2014-2015 Sebastian McKenzie
?