-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathprepExercises.js
114 lines (71 loc) · 3.34 KB
/
prepExercises.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
// Exercises
// Basic Requirments
// 1.Summation to n: Let's implement the function sum that takes a single parameter n, and computes the sum of all integers up to n starting from 0, e.g.:
// function sum(n) {
// // TODO: your code here
// }
// sum(3); // => 6
// sum(4); // => 10
// sum(5); // => 15
// 2.Factorial of n: The factorial of n is the product of all the integers preceding n, starting with 1, e.g.:
// function factorial(n) {
// // TODO: your code here
// }
// factorial(3); // => 6
// factorial(4); // => 24
// factorial(5); // => 120
// 3.Repeating a String n Times: Let's write a function called repeatString that takes two parameters: a string str, which is the string to be repeated, and count -- a number representing how many times the string s should be repeated, e.g.
// function repeatString(str, count) {
// // TODO: your code here
// }
// repeatString('dog', 0); // => ''
// repeatString('dog', 1); // => 'dog'
// repeatString('dog', 2); // => 'dogdog'
// repeatString('dog', 3); // => 'dogdogdog'
// Your task is to implement the repeatString function using a while loop.
// More Practice
// 1.Go back through any of the exercises done in the introduction to repetition and refactor them to use the while loop.
// 2.Fencepost Loop: Use a while loop to build a single string with the numbers 1 through n, separated by commas. Have it return the new string. How can we make sure not to have a comma after the last number?
// function counting(n) {
// // TODO: your code here
// }
// counting(5); // => '1, 2, 3, 4, 5'
// counting(1); // => '1'
// counting(3); // => '1, 2, 3'
// 3.Meet & Greet: n numbers are coming to a party for numbers. Introduce them each to all the guests as they arrive. You should end up with output looking something like this one for n = 5:
// welcome 1
// welcome 2, meet 1
// welcome 3, meet 1 and 2
// welcome 4, meet 1, 2 and 3
// welcome 5, meet 1, 2, 3 and 4
// HINT: search for which symbol that creates a line break in a string.
// 4.Reverse String: Write a function that takes a string as an input and returns the reverse of that string.
// function reverseString(str) {
// // TODO: your code here
// }
// reverseString('hello'); // => 'olleh'
// reverseString('1#*$'); // => '$*#1'
// Do this using a while loop.
// 5.Write a javascript program to print all Prime numbers from 1 to n.
// 6.Write a JavaScript for loop that will iterate from 0 to n. For each iteration, it will check if the current number is odd or even, and display a message to the screen.
// Sample Output :
// '0 is even'
// '1 is odd'
// '2 is even'
// 7.Write a javascript program to enter any number and print all factors of the number.
// Advanced
// 1. Write a javascript function to find the sum of first and last digit of any number, without convert it to string
// firstAndLast(456) ==> 4 + 6 = 11
// firstAndLast(123) ==> 1 + 3 = 4
// firstAndLast(197) ==> 1 + 7 = 8
// 2.Write a javascript program to draw the stars
// *
// ***
// *****
// *******
// *********
// *******
// *****
// ***
// *
// 3. Write a program that reads a character for playing the game of Rock-Paper-Scissors. If the character entered by the user is not one of 'P', 'R' or 'S', the program keeps on prompting the user to enter a new character.