From 7a1989c11f8cb522cdcd91edbcdb371816b65834 Mon Sep 17 00:00:00 2001 From: Tien Hoang Pham Date: Tue, 2 Apr 2019 00:33:12 +0300 Subject: [PATCH 1/6] Add exercise files --- students/hoang-pham/02-04-2019/ex1.js | 1 - students/hoang-pham/03-04-2019/ex1.js | 1 - students/hoang-pham/03-04-2019/ex2.js | 1 - students/rename-this-folder/ex1.js | 9 +++++++++ students/rename-this-folder/ex2.js | 11 +++++++++++ students/rename-this-folder/ex3.js | 10 ++++++++++ students/rename-this-folder/ex4.js | 9 +++++++++ 7 files changed, 39 insertions(+), 3 deletions(-) delete mode 100644 students/hoang-pham/02-04-2019/ex1.js delete mode 100644 students/hoang-pham/03-04-2019/ex1.js delete mode 100644 students/hoang-pham/03-04-2019/ex2.js create mode 100644 students/rename-this-folder/ex1.js create mode 100644 students/rename-this-folder/ex2.js create mode 100644 students/rename-this-folder/ex3.js create mode 100644 students/rename-this-folder/ex4.js diff --git a/students/hoang-pham/02-04-2019/ex1.js b/students/hoang-pham/02-04-2019/ex1.js deleted file mode 100644 index 4b63607..0000000 --- a/students/hoang-pham/02-04-2019/ex1.js +++ /dev/null @@ -1 +0,0 @@ -console.log('test content for ex1'); diff --git a/students/hoang-pham/03-04-2019/ex1.js b/students/hoang-pham/03-04-2019/ex1.js deleted file mode 100644 index 4b63607..0000000 --- a/students/hoang-pham/03-04-2019/ex1.js +++ /dev/null @@ -1 +0,0 @@ -console.log('test content for ex1'); diff --git a/students/hoang-pham/03-04-2019/ex2.js b/students/hoang-pham/03-04-2019/ex2.js deleted file mode 100644 index e536cc2..0000000 --- a/students/hoang-pham/03-04-2019/ex2.js +++ /dev/null @@ -1 +0,0 @@ -console.log('test content for ex2'); diff --git a/students/rename-this-folder/ex1.js b/students/rename-this-folder/ex1.js new file mode 100644 index 0000000..1c7fc89 --- /dev/null +++ b/students/rename-this-folder/ex1.js @@ -0,0 +1,9 @@ +/* +Write a JavaScript function to check whether an input is an array or not. + +Test Data : +console.log(isArray('w3resource')); // false +console.log(isArray([1, 2, 4, 0])); // true +*/ + + diff --git a/students/rename-this-folder/ex2.js b/students/rename-this-folder/ex2.js new file mode 100644 index 0000000..eb24fa7 --- /dev/null +++ b/students/rename-this-folder/ex2.js @@ -0,0 +1,11 @@ +/* +Write a JavaScript function to get the first element of an array. Passing a parameter 'n' will return the first 'n' elements of the array. + +Test Data :  +console.log(first([7, 9, 0, -2])); // 7 +console.log(first([],3)); // [] +console.log(first([7, 9, 0, -2],3)); // [7,9,0] +console.log(first([7, 9, 0, -2],6)); // [7, 9, 0, -2] +console.log(first([7, 9, 0, -2],-3)); // [] + +*/ \ No newline at end of file diff --git a/students/rename-this-folder/ex3.js b/students/rename-this-folder/ex3.js new file mode 100644 index 0000000..eb98a2a --- /dev/null +++ b/students/rename-this-folder/ex3.js @@ -0,0 +1,10 @@ +/* +Write a simple JavaScript program to join all elements of the following array into a string. + +Sample array : +var myText = ['This', 'is', 'not', 'working']; +joinArray(myText); + +Expected Output :  +This is not working + */ \ No newline at end of file diff --git a/students/rename-this-folder/ex4.js b/students/rename-this-folder/ex4.js new file mode 100644 index 0000000..72f98cc --- /dev/null +++ b/students/rename-this-folder/ex4.js @@ -0,0 +1,9 @@ +/* +ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but exactly 4 digits or exactly 6 digits. If the function is passed a valid PIN string, return true, else return false. + +validatePIN(1234); // false +validatePIN(00004324); // false +validatePIN('2312'); // true +validatePin('asd123'); // false +validatePin('000010'); // true + */ \ No newline at end of file From 4c7ab8a21db34a8cd97f098a282ffcb8d53511d4 Mon Sep 17 00:00:00 2001 From: Tien Hoang Pham Date: Tue, 2 Apr 2019 00:45:44 +0300 Subject: [PATCH 2/6] rename exercise folder to copy-and-rename --- students/{rename-this-folder => copy-and-rename}/ex1.js | 0 students/{rename-this-folder => copy-and-rename}/ex2.js | 0 students/{rename-this-folder => copy-and-rename}/ex3.js | 0 students/{rename-this-folder => copy-and-rename}/ex4.js | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename students/{rename-this-folder => copy-and-rename}/ex1.js (100%) rename students/{rename-this-folder => copy-and-rename}/ex2.js (100%) rename students/{rename-this-folder => copy-and-rename}/ex3.js (100%) rename students/{rename-this-folder => copy-and-rename}/ex4.js (100%) diff --git a/students/rename-this-folder/ex1.js b/students/copy-and-rename/ex1.js similarity index 100% rename from students/rename-this-folder/ex1.js rename to students/copy-and-rename/ex1.js diff --git a/students/rename-this-folder/ex2.js b/students/copy-and-rename/ex2.js similarity index 100% rename from students/rename-this-folder/ex2.js rename to students/copy-and-rename/ex2.js diff --git a/students/rename-this-folder/ex3.js b/students/copy-and-rename/ex3.js similarity index 100% rename from students/rename-this-folder/ex3.js rename to students/copy-and-rename/ex3.js diff --git a/students/rename-this-folder/ex4.js b/students/copy-and-rename/ex4.js similarity index 100% rename from students/rename-this-folder/ex4.js rename to students/copy-and-rename/ex4.js From f16de4fa7958acb4bd49073b131b9b1ce6b5a99f Mon Sep 17 00:00:00 2001 From: Tien Hoang Pham Date: Tue, 2 Apr 2019 12:28:35 +0300 Subject: [PATCH 3/6] Add sample solutions --- students/test-student/ex1.js | 12 ++++++++++++ students/test-student/ex2.js | 29 +++++++++++++++++++++++++++++ students/test-student/ex3.js | 26 ++++++++++++++++++++++++++ students/test-student/ex4.js | 21 +++++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 students/test-student/ex1.js create mode 100644 students/test-student/ex2.js create mode 100644 students/test-student/ex3.js create mode 100644 students/test-student/ex4.js diff --git a/students/test-student/ex1.js b/students/test-student/ex1.js new file mode 100644 index 0000000..dfee6ff --- /dev/null +++ b/students/test-student/ex1.js @@ -0,0 +1,12 @@ +/* +Write a JavaScript function to check whether an input is an array or not. + +Test Data : +console.log(isArray('w3resource')); // false +console.log(isArray([1, 2, 4, 0])); // true +*/ + +function isArray(input){ + return Array.isArray(input); +} + diff --git a/students/test-student/ex2.js b/students/test-student/ex2.js new file mode 100644 index 0000000..8f03f61 --- /dev/null +++ b/students/test-student/ex2.js @@ -0,0 +1,29 @@ +/* +Write a JavaScript function to get the first element of an array. Passing a parameter 'n' will return the first 'n' elements of the array. + +Test Data :  +console.log(first([7, 9, 0, -2])); // 7 +console.log(first([],3)); // [] +console.log(first([7, 9, 0, -2],3)); // [7,9,0] +console.log(first([7, 9, 0, -2],6)); // [7, 9, 0, -2] +console.log(first([7, 9, 0, -2],-3)); // [] + +*/ +function first(input1, input2){ + if (!Array.isArray(input1)) { + return; + } else if (input1 === []) { // array can be empty >> return empty array + return []; + //second parameter is not given >> return the first element of the array + } else if (typeof(input2) === 'undefined' || input2 === null) { + return input1[0]; + // array has less number of elements than requested >> return all possibilities + } else if (input1.length <= input2) { + return input1; + } else if (input2 < 0) { //second parameter is negative >> return emtpy array + return []; + } else { + return input1.splice(0, input2); + } +} + diff --git a/students/test-student/ex3.js b/students/test-student/ex3.js new file mode 100644 index 0000000..648b77d --- /dev/null +++ b/students/test-student/ex3.js @@ -0,0 +1,26 @@ +/* +Write a simple JavaScript program to join all elements of the following array into a string. + +Sample array : +var myText = ['This', 'is', 'not', 'working']; +joinArray(myText); + +Expected Output :  +This is not working + */ + +function joinArray(input) { + var index = 0, + result = ''; + + while(index < input.length){ + result = result + ' ' + input[index]; + index++; + } + + return result; +} + +function joinArray(input) { + return input.join(' '); +} \ No newline at end of file diff --git a/students/test-student/ex4.js b/students/test-student/ex4.js new file mode 100644 index 0000000..124b26b --- /dev/null +++ b/students/test-student/ex4.js @@ -0,0 +1,21 @@ +/* +ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but exactly 4 digits or exactly 6 digits. If the function is passed a valid PIN string, return true, else return false. + +validatePIN(1234); // false +validatePIN(00004324); // false +validatePIN('2312'); // true +validatePin('asd123'); // false +validatePin('000010'); // true + */ + +function validatePIN(input) { + if (typeof(input) !== 'string') { + return false; + } + + // it is a number Number(input) returns false if it is not entirely a number + // make sure it contains no dot input.indexOf('.') returns value of greater + // than -1 if found a dot + // make sure it has length of either 4 or 6 + // input.length === 4 || input.length === 6 +} From 93863c4ef4b8b3fdc86b8595f5273c70f32d7356 Mon Sep 17 00:00:00 2001 From: Tien Hoang Pham Date: Wed, 3 Apr 2019 10:27:12 +0300 Subject: [PATCH 4/6] add exercise files --- students/copy-and-rename/ex1-sample.js | 0 students/copy-and-rename/ex1.js | 9 ----- students/copy-and-rename/ex2.js | 11 ------ students/copy-and-rename/ex3.js | 10 ------ students/copy-and-rename/ex4.js | 9 ----- students/copy-and-rename/questions.txt | 50 ++++++++++++++++++++++++++ students/test-student/ex1.js | 12 ------- students/test-student/ex2.js | 29 --------------- students/test-student/ex3.js | 26 -------------- students/test-student/ex4.js | 21 ----------- 10 files changed, 50 insertions(+), 127 deletions(-) create mode 100644 students/copy-and-rename/ex1-sample.js delete mode 100644 students/copy-and-rename/ex1.js delete mode 100644 students/copy-and-rename/ex2.js delete mode 100644 students/copy-and-rename/ex3.js delete mode 100644 students/copy-and-rename/ex4.js create mode 100644 students/copy-and-rename/questions.txt delete mode 100644 students/test-student/ex1.js delete mode 100644 students/test-student/ex2.js delete mode 100644 students/test-student/ex3.js delete mode 100644 students/test-student/ex4.js diff --git a/students/copy-and-rename/ex1-sample.js b/students/copy-and-rename/ex1-sample.js new file mode 100644 index 0000000..e69de29 diff --git a/students/copy-and-rename/ex1.js b/students/copy-and-rename/ex1.js deleted file mode 100644 index 1c7fc89..0000000 --- a/students/copy-and-rename/ex1.js +++ /dev/null @@ -1,9 +0,0 @@ -/* -Write a JavaScript function to check whether an input is an array or not. - -Test Data : -console.log(isArray('w3resource')); // false -console.log(isArray([1, 2, 4, 0])); // true -*/ - - diff --git a/students/copy-and-rename/ex2.js b/students/copy-and-rename/ex2.js deleted file mode 100644 index eb24fa7..0000000 --- a/students/copy-and-rename/ex2.js +++ /dev/null @@ -1,11 +0,0 @@ -/* -Write a JavaScript function to get the first element of an array. Passing a parameter 'n' will return the first 'n' elements of the array. - -Test Data :  -console.log(first([7, 9, 0, -2])); // 7 -console.log(first([],3)); // [] -console.log(first([7, 9, 0, -2],3)); // [7,9,0] -console.log(first([7, 9, 0, -2],6)); // [7, 9, 0, -2] -console.log(first([7, 9, 0, -2],-3)); // [] - -*/ \ No newline at end of file diff --git a/students/copy-and-rename/ex3.js b/students/copy-and-rename/ex3.js deleted file mode 100644 index eb98a2a..0000000 --- a/students/copy-and-rename/ex3.js +++ /dev/null @@ -1,10 +0,0 @@ -/* -Write a simple JavaScript program to join all elements of the following array into a string. - -Sample array : -var myText = ['This', 'is', 'not', 'working']; -joinArray(myText); - -Expected Output :  -This is not working - */ \ No newline at end of file diff --git a/students/copy-and-rename/ex4.js b/students/copy-and-rename/ex4.js deleted file mode 100644 index 72f98cc..0000000 --- a/students/copy-and-rename/ex4.js +++ /dev/null @@ -1,9 +0,0 @@ -/* -ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but exactly 4 digits or exactly 6 digits. If the function is passed a valid PIN string, return true, else return false. - -validatePIN(1234); // false -validatePIN(00004324); // false -validatePIN('2312'); // true -validatePin('asd123'); // false -validatePin('000010'); // true - */ \ No newline at end of file diff --git a/students/copy-and-rename/questions.txt b/students/copy-and-rename/questions.txt new file mode 100644 index 0000000..1430e34 --- /dev/null +++ b/students/copy-and-rename/questions.txt @@ -0,0 +1,50 @@ +1. Write a JavaScript program that accept two integers and display the larger. +2. Write a JavaScript conditional statement to find the sign of product of three numbers. Display an alert box with the specified sign. +Sample numbers : 3, -7, 2 +Output : The sign is - +3. Write a JavaScript conditional statement to sort three numbers. Display an alert box to show the result. +Sample numbers : 0, -1, 4 +Output : 4, 0, -1 +4. Write a JavaScript conditional statement to find the largest of five numbers. Display an alert box to show the result. +Sample numbers : -5, -2, -6, 0, -1 +Output : 0 +5. Write a JavaScript for loop that will iterate from 0 to 15. 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" +---------- +---------- +6. Write a JavaScript program which compute, the average marks of the following students Then, this average is used to determine the corresponding grade. + +Student Name Marks +David 80 +Vinoth 77 +Divya 88 +Ishitha 95 +Thomas 68 + +The grades are computed as follows: +Range Grade +<60 F +<70 D +<80 C +<90 B +<100 A + +7. Write a JavaScript program which iterates the integers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". +8. According to Wikipedia a happy number is defined by the following process: +"Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers)". +Write a JavaScript program to find and print the first 5 happy numbers. +9. Write a JavaScript program to find the armstrong numbers of 3 digits. +Note: An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371. +10. Write a JavaScript program to construct the following pattern, using a nested for loop. +* +* * +* * * +* * * * +* * * * * +11. Write a JavaScript program to compute the greatest common divisor (GCD) of two positive integers. +12. Write a JavaScript program to sum the multiples of 3 and 5 under 1000. + + diff --git a/students/test-student/ex1.js b/students/test-student/ex1.js deleted file mode 100644 index dfee6ff..0000000 --- a/students/test-student/ex1.js +++ /dev/null @@ -1,12 +0,0 @@ -/* -Write a JavaScript function to check whether an input is an array or not. - -Test Data : -console.log(isArray('w3resource')); // false -console.log(isArray([1, 2, 4, 0])); // true -*/ - -function isArray(input){ - return Array.isArray(input); -} - diff --git a/students/test-student/ex2.js b/students/test-student/ex2.js deleted file mode 100644 index 8f03f61..0000000 --- a/students/test-student/ex2.js +++ /dev/null @@ -1,29 +0,0 @@ -/* -Write a JavaScript function to get the first element of an array. Passing a parameter 'n' will return the first 'n' elements of the array. - -Test Data :  -console.log(first([7, 9, 0, -2])); // 7 -console.log(first([],3)); // [] -console.log(first([7, 9, 0, -2],3)); // [7,9,0] -console.log(first([7, 9, 0, -2],6)); // [7, 9, 0, -2] -console.log(first([7, 9, 0, -2],-3)); // [] - -*/ -function first(input1, input2){ - if (!Array.isArray(input1)) { - return; - } else if (input1 === []) { // array can be empty >> return empty array - return []; - //second parameter is not given >> return the first element of the array - } else if (typeof(input2) === 'undefined' || input2 === null) { - return input1[0]; - // array has less number of elements than requested >> return all possibilities - } else if (input1.length <= input2) { - return input1; - } else if (input2 < 0) { //second parameter is negative >> return emtpy array - return []; - } else { - return input1.splice(0, input2); - } -} - diff --git a/students/test-student/ex3.js b/students/test-student/ex3.js deleted file mode 100644 index 648b77d..0000000 --- a/students/test-student/ex3.js +++ /dev/null @@ -1,26 +0,0 @@ -/* -Write a simple JavaScript program to join all elements of the following array into a string. - -Sample array : -var myText = ['This', 'is', 'not', 'working']; -joinArray(myText); - -Expected Output :  -This is not working - */ - -function joinArray(input) { - var index = 0, - result = ''; - - while(index < input.length){ - result = result + ' ' + input[index]; - index++; - } - - return result; -} - -function joinArray(input) { - return input.join(' '); -} \ No newline at end of file diff --git a/students/test-student/ex4.js b/students/test-student/ex4.js deleted file mode 100644 index 124b26b..0000000 --- a/students/test-student/ex4.js +++ /dev/null @@ -1,21 +0,0 @@ -/* -ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but exactly 4 digits or exactly 6 digits. If the function is passed a valid PIN string, return true, else return false. - -validatePIN(1234); // false -validatePIN(00004324); // false -validatePIN('2312'); // true -validatePin('asd123'); // false -validatePin('000010'); // true - */ - -function validatePIN(input) { - if (typeof(input) !== 'string') { - return false; - } - - // it is a number Number(input) returns false if it is not entirely a number - // make sure it contains no dot input.indexOf('.') returns value of greater - // than -1 if found a dot - // make sure it has length of either 4 or 6 - // input.length === 4 || input.length === 6 -} From 63acffebac6be0415c7071b1e4a47c1587ab02c6 Mon Sep 17 00:00:00 2001 From: Locklin Doug Date: Wed, 3 Apr 2019 20:13:07 +0300 Subject: [PATCH 5/6] My afternoon work, exs. 1, 5, 12 --- students/Doug-Locklin/ex1-sample.js | 42 ++++++++++++++++++++++++ students/Doug-Locklin/questions.txt | 50 +++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 students/Doug-Locklin/ex1-sample.js create mode 100644 students/Doug-Locklin/questions.txt diff --git a/students/Doug-Locklin/ex1-sample.js b/students/Doug-Locklin/ex1-sample.js new file mode 100644 index 0000000..8cd1fca --- /dev/null +++ b/students/Doug-Locklin/ex1-sample.js @@ -0,0 +1,42 @@ +//1. Write a JavaScript program that accept two integers and display the larger. +function displayLarger(num1, num2){ + if (num1 === num2){ + console.log("Invalid input, the numbers are equal."); + } + else if (num1>num2){ + console.log(num1); + } + else { + console.log(num2); + } +} + +displayLarger(34,39); + +/*5. Write a JavaScript for loop that will iterate from 0 to 15. 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" +---------- +---------- */ +var num1 = 0; +for (num1 = 0; num1 < 16; num1++) { + if (step % 2 == 0){ + console.log(`${num1} is even.`) + } + + else { + console.log(`${num1} is odd.`) + } + } + + //12. Write a JavaScript program to sum the multiples of 3 and 5 under 1000. + var myArray1=[]; + for (z=0; z<1000; z++){ + if ((z % 3==0)&&(z % 5==0)) { + myArray1.push(z); + } + } +var answer = myArray1.reduce((partial_sum, a) => partial_sum + a); +console.log(`${answer} is the sum of all the numbers divisible by 3 and 5 under 1000.`); \ No newline at end of file diff --git a/students/Doug-Locklin/questions.txt b/students/Doug-Locklin/questions.txt new file mode 100644 index 0000000..1430e34 --- /dev/null +++ b/students/Doug-Locklin/questions.txt @@ -0,0 +1,50 @@ +1. Write a JavaScript program that accept two integers and display the larger. +2. Write a JavaScript conditional statement to find the sign of product of three numbers. Display an alert box with the specified sign. +Sample numbers : 3, -7, 2 +Output : The sign is - +3. Write a JavaScript conditional statement to sort three numbers. Display an alert box to show the result. +Sample numbers : 0, -1, 4 +Output : 4, 0, -1 +4. Write a JavaScript conditional statement to find the largest of five numbers. Display an alert box to show the result. +Sample numbers : -5, -2, -6, 0, -1 +Output : 0 +5. Write a JavaScript for loop that will iterate from 0 to 15. 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" +---------- +---------- +6. Write a JavaScript program which compute, the average marks of the following students Then, this average is used to determine the corresponding grade. + +Student Name Marks +David 80 +Vinoth 77 +Divya 88 +Ishitha 95 +Thomas 68 + +The grades are computed as follows: +Range Grade +<60 F +<70 D +<80 C +<90 B +<100 A + +7. Write a JavaScript program which iterates the integers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". +8. According to Wikipedia a happy number is defined by the following process: +"Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers)". +Write a JavaScript program to find and print the first 5 happy numbers. +9. Write a JavaScript program to find the armstrong numbers of 3 digits. +Note: An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371. +10. Write a JavaScript program to construct the following pattern, using a nested for loop. +* +* * +* * * +* * * * +* * * * * +11. Write a JavaScript program to compute the greatest common divisor (GCD) of two positive integers. +12. Write a JavaScript program to sum the multiples of 3 and 5 under 1000. + + From f8594ddfd5ed22ac930a4960412f54fd961c1021 Mon Sep 17 00:00:00 2001 From: Douglas Locklin Date: Thu, 4 Apr 2019 23:47:39 +0300 Subject: [PATCH 6/6] Update js example with more solutions --- students/Doug-Locklin/ex1-sample.js | 143 +++++++++++++++++++++++++++- 1 file changed, 140 insertions(+), 3 deletions(-) diff --git a/students/Doug-Locklin/ex1-sample.js b/students/Doug-Locklin/ex1-sample.js index 8cd1fca..c9a64a2 100644 --- a/students/Doug-Locklin/ex1-sample.js +++ b/students/Doug-Locklin/ex1-sample.js @@ -13,24 +13,161 @@ function displayLarger(num1, num2){ displayLarger(34,39); -/*5. Write a JavaScript for loop that will iterate from 0 to 15. For each iteration, it will check if the current number is odd or even, and display a message to the screen. +/*2. Write a JavaScript conditional statement to find the sign of product of three numbers. +Display an alert box with the specified sign. +Sample numbers : 3, -7, 2 +Output : The sign is - */ + +var signOfNums = (num1, num2, num3) => { + productNums = (num1*num2*num3); + if (productNums < 0){ + alert ('The sign of the product is negative.') + } else if (productNums >= 0){ + alert ('The sign of the product is positive.') + } +}; + +signOfNums(3,-7,2); + +/*3. Write a JavaScript conditional statement to sort three numbers. Display an alert box to show the result. +Sample numbers : 0, -1, 4 +Output : 4, 0, -1 */ + +var sortInputedNumbers = (num1, num2, num3) =>{ + inputedNumbers = [num1, num2, num3]; + inputedNumbers.sort((a, b) => a - b); + alert(inputedNumbers.reverse()); +} + +sortInputedNumbers(0, -1, 4); + +/*4. Write a JavaScript conditional statement to find the largest of five numbers. Display an alert box to show the result. +Sample numbers : -5, -2, -6, 0, -1 +Output : 0 */ + +var findLargestNumber = (num1, num2, num3, num4, num5) => { + inputedNumbers = [num1, num2, num3, num4, num5]; + inputedNumbers.sort((a, b) => a - b); + alert(inputedNumbers[4]); +} + +findLargestNumber(-5, -2, -6, 0, -1 ); + +/*5. Write a JavaScript for loop that will iterate from 0 to 15. +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" ---------- ---------- */ + var num1 = 0; for (num1 = 0; num1 < 16; num1++) { if (step % 2 == 0){ console.log(`${num1} is even.`) } - else { console.log(`${num1} is odd.`) } } +/*6. Write a JavaScript program which compute, the average marks of the following students. +Then, this average is used to determine the corresponding grade. + +Student Name Marks +David 80 +Vinoth 77 +Divya 88 +Ishitha 95 +Thomas 68 + +The grades are computed as follows: +Range Grade +<60 F +<70 D +<80 C +<90 B +<100 A*/ + +var students = [ + {name: 'David', grade: 80}, + {name: 'Vinoth', grade: 77}, + {name: 'Divya', grade: 88}, + {name: 'Ishitha', grade: 95}, + {name: 'Thomas', grade: 68}, +]; + +students.forEach(function (student) { + if (student.grade>= 90) { + console.log(`${student.name}'s grade is A.`) + } + else if (student.grade>= 80) { + console.log(`${student.name}'s grade is B.`) + } + else if (student.grade>= 70) { + console.log(`${student.name}'s grade is C.`) + } + else if (student.grade>= 60) { + console.log(`${student.name}'s grade is D.`) + } + else if (student.grade< 60){ + console.log(`${student.name}'s grade is F.`) + } +}); + +/* +7. Write a JavaScript program which iterates the integers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". */ + +var myArray1=[]; + for (z=1; z<101; z++){ + if ((z % 3==0)&&(z % 5==0)) { + myArray1.push('FizzBuzz'); + } else if (z % 3==0){ + myArray1.push('Fizz'); + } else if (z % 5==0){ + myArray1.push('Buzz'); + } else { + myArray1.push(z); + } + } +console.log(myArray1); + +/* +8. According to Wikipedia a happy number is defined by the following process: +"Starting with any positive integer, replace the number by the sum of the squares of its digits, +and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in +a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, +while those that do not end in 1 are unhappy numbers (or sad numbers)". +Write a JavaScript program to find and print the first 5 happy numbers. */ + + +/* +9. Write a JavaScript program to find the armstrong numbers of 3 digits. +Note: An Armstrong number of three digits is an integer such that the sum of the cubes of its digits +is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371. +*/ + + +/* +10. Write a JavaScript program to construct the following pattern, using a nested for loop. +* +* * +* * * +* * * * +* * * * * + +*/ + + + +/* +11. Write a JavaScript program to compute the greatest common divisor (GCD) of two positive integers. */ + + + //12. Write a JavaScript program to sum the multiples of 3 and 5 under 1000. var myArray1=[]; for (z=0; z<1000; z++){ @@ -39,4 +176,4 @@ for (num1 = 0; num1 < 16; num1++) { } } var answer = myArray1.reduce((partial_sum, a) => partial_sum + a); -console.log(`${answer} is the sum of all the numbers divisible by 3 and 5 under 1000.`); \ No newline at end of file +console.log(`${answer} is the sum of all the numbers divisible by 3 and 5 under 1000.`);