JavaScript Exercise[http://www.w3resource.com/javascript-exercises/]
- Write a JavaScript program to display the current day and time in the following format.
var today = new Date();
var day = today.getDay();
var dayList = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
console.log('Today is: ' + dayList[day]);
var hour = today.getHours();
var prepand = (hour >= 12) ? 'AM' : 'PM';
if(hour > 12) {
hour = hour - 12;
}
var min = today.getMinutes();
var sec = today.getSeconds();
console.log('Current time is: ' + hour + prepand + ':' + min + ':' + sec);
- Write a JavaScript function that reverse a number.
function reverse(value) {
return parseInt(value.toString().split('').reverse().join(''));
}
OR
function reverse(value) {
var result = 0;
var quotient = 0;
var remains = 0;
while(value > 0) {
quotient = Math.floor(value / 10);
remains = value % 10;
result = result * 10 + remains;
value = quotient;
}
return result;
}
- Write a JavaScript function that checks whether a passed string is palindrome or not?
function checkPalindrome(strs) {
if(strs === '') {
return false;
}
var length = strs.length;
var start = 0;
var end = length - 1;
while(start < end) {
if(strs.charAt(start) != strs.charAt(end)) {
return false;
break;
}
start++;
end--;
}
return true;
}
- Write a JavaScript function that generates all combinations of a string.
function combinations(str) {
var final = [];
var result = [];
for(var i = 0; i < str.length; i++) {
var currentChar = str.charAt(i);
if(result.indexOf(currentChar) < 0) {
var resultLength = result.length;
for(var j = 0; j < resultLength; j++) {
result[j] = result[j].concat(currentChar);
}
result.push(currentChar);
final = final.concat(result);
}
}
return final;
}
var TreeNode = function () { this.left = null; this.right = null; this.value = 0; };
var tree = new TreeNode(); tree.left = new TreeNode(); tree.right = new TreeNode(); tree.value = 10; tree.left.value = 1; tree.right.value = 2; tree.right.right = new TreeNode(); tree.right.right.value = 3; tree.right.left = new TreeNode(); tree.right.left.value = 4;
function sumNodes(rootNode) {
if(rootNode.left == null && rootNode.right == null) {
return rootNode.value;
}
if(rootNode.left != null && rootNode.right == null) {
return rootNode.value + sumNodes(rootNode.left);
}
if(rootNode.left == null && rootNode.right != null) {
return rootNode.value + sumNodes(rootNode.right);
}
return rottNode.value + sumNodes(rootNode.left) + sumNodes(rootNode.right);
}
- Implement a function analyze(array, length) that has following functionalities. // Given any integer array e.g.
// int fred[]={1 2 3 5 7 2 7 4 11}; // analyze(fred,9);
// Analyze this simple data set and find the following quantities: // - minimum and maximum integer // - missing values within the range // - duplicated values and the number of duplicates // - mean of the values // - [do last] extra credit: standard deviation of the values
// example input and output:
// [devo]$ ./foo 1 2 3 5 7 2 7 4 11 // Min value is 1 // Max value is 11 // Mean value is 4.67 // Standard deviation is 3.20 // Integer 2 present 2 times // Integer 6 not present // Integer 7 present 2 times // Integer 8 not present // Integer 9 not present // Integer 10 not present
function analyze(array, length){
var min = Min(array); // done
var max = Max(array); // done
var std = standardDeviation(array); // done
var map = new Map();
console.log('Min value is ' + min);
console.log('Max value is ' + max);
console.log('Standard deviation is ' + std);
array.sort((a, b) => {
return a - b;
}); // done
var index;
var i;
for(i = min; i <= max; i++) {
if(array.includes(i)) {
index = array.indexOf(i);
while(i == array[index]){
if(map.has(array[index])){
map.set(array[index], map.get(array[index])+ 1);
} else {
map.set(i, 1);
}
index++;
}
} else {
map.set(i, 0);
}
}
var iterator = map.entries();
var current = iterator.next();
while(current.done != true) {
if(current.value[1] == 0) {
console.log('Integer ' + current.value[0] + ' not present.');
} else {
console.log('Integer ' + current.value[0] + ' present ' + current.value[1] + ' times.');
}
current = iterator.next();
}
}
function standardDeviation(values){
var avg = average(values);
var squareDiffs = values.map(function(value){
var diff = value - avg;
var sqrDiff = diff * diff;
return sqrDiff;
});
var avgSquareDiff = average(squareDiffs);
var stdDev = Math.sqrt(avgSquareDiff);
return stdDev;
}
function average(data){
var sum = data.reduce(function(sum, value){
return sum + value;
}, 0);
var avg = sum / data.length;
return avg;
}
function Min(array){
var min = array[0];
for(var index in array) {
min = min < array[index] ? min : array[index];
}
return min;
}
function Max(array) {
var max = array[0];
for(var index in array){
max = max > array[index] ? max : array[index];
}
return max;
}