Javascript Everywhere

Help for javascript developer

Javascript Everywhere in the world

Tuesday, April 28, 2020

JavaScript Challenges: Count a string and add more character if a string is less than 25 character

Count a string and add remain character
Make string fix size and fill with another character

Introduction:

    Many javascript developers are missing functions provided by javascript. This is a simple program just append and prepend string if the given string length is less then 25. Many developers try with loop and long logic for this. Our question is why we not using built-in functions. Let me try with built-in functions.

Challenge:

   Write a logic for below input:
Input Output
Javascript Everywhere Javascript Everywhere----
~~~~Javascript Everywhere
You are a good developer You are a good developer-
~You are a good developer
New New----------------------
~~~~~~~~~~~~~~~~~~~~~~New
Challenges Challenges---------------
~~~~~~~~~~~~~~~Challenges

Program Description: Write a program if the string length is 25 characters then add ~(tilde) sign at prefix and add -(dash) sign at postfix.

The program output is:
1)

function stringModify(str) {
    console.log(str.padEnd(25, '-'));
    console.log(str.padStart(25, '~'));
}

Another program:
2)

function stringModify(str){
   const sign1="-";
   const sign2="~";
   let output=str.length>25 ? str: str.concat(sign1.repeat(25 - str.length), '\n', sign2.repeat(25 - str.length), str);
   return output;
}
console.log(stringModify("New"));

Explanation:
1) In the first program, we use the built-in function provided by javascript.
2) The function is padEnd and padStart

padEnd()

This is a string function. The padEnd() method adds a current string in the given string. So the result is reached to the given length. String added at end of the current string. Ex:

const str1 = 'Welcome to javascript';

console.log(str1.padEnd(25, '_'));
// expected output: "Welcome to javascript____"

const str2 = '1000';

console.log(str2.padEnd(5));
// expected output: "1000 "

The default replaces string is space.

padStart()

This is also a string function. The padStart() method also expand string. So the result is reached to the given length. String added at the starting of the current string. Ex:

const strTemp = 'Welcome to javascript';

console.log(strTemp.padStart(25, '_'));
// expected output: "____Welcome to javascript"

const numberVal = '500';

console.log(numberVal.padEnd(5));
// expected output: "  500"

The default replaces character is space.
Many ways you can do this but most useful and preferable is using the build-in function.
Other: Swap to a variable without using the third one. more than 3 solutions provided.
Swap to a variable without the help of the third variable

Monday, April 27, 2020

JavaScript Challenges: How to swap two variable without using third one?

Without third variable swap to variable
Swap to variable in javascript without using the third variable

Introduction:
    Many javascript developers ask a question, How many ways to swap two variables without using third. Our answer is there are many ways we can achieve this but we always go with the easiest and fast solution. Every language provides a different way of doing the swap variables.

Challenge:

Challenge is declared two variable and swap value between without using the third variable like

Input: var a = 10;
           var b = 20;
          console.log(a, b) is give output like (10, 20)
Output: console.log(a, b) is output like (20, 10)

The logic of this program is many, we provide the best solution here:
The program output is:
1)

var a = 10;
var b = 20;
[a, b] = [b, a];
console.log(a, b);
2)
let a = 10;
let b = 20;
a = a * b;
b = a / b;
a = a / b;
console.log(a, b)
3)

var a = 10;
var b = 20;
console.log(a,b);
a = new Array(a,b);
b = a[0];
a = a[1];
console.log(a,b);

Explanation: There are many ways you can perform this program. You are free to ask question and comment, I will reply and give you a proper solution.


Other: Reverse string without using the built-in function

Sunday, April 26, 2020

JavaScript Challenges: Reverse string without any built in function

How to reverse a string without using function
Reverse string without using the built-in function

Introduction:
    Every javascript function has some logic for modification like toLowerCase(), toUpperCase(), join() and etc.. Today we are going to perform one program is revere string without any built-in function. Many developers doing this.

Challenge:

   Write a logic for below input:
Input Output
Javascript Everywhere erehwyrevE tpirsavaJ
Hello Developer repoleveD olleH
Was it a car or a cat I saw? ?was i tac ro rac a ti saW

Program Description: Write an algorithm to reverse a string without using any function provided by javascript. like reverse(), join(). Only allow length() is you really need.

The program output is:
1)

function reverseString(str) {
    var reversedString = '';
    for(let i= str.length - 1; i >= 0; i--)
    {
        reversedString += str[i];
    }
    return reversedString;
}

Another program with reverse and other function:
2)

"Javascript Developer improve skill here".split('').reverse().join('');
3)

"Jsgrip is website for developer".split('').reduce((temp1, temp2) => temp2 + temp1);

Explanation: Using array creates one variable blank and then inside for loop starting from the length of string and end to 0. From the last index assign value to the variable and after a loop just return that variable.

There are another way as well you can do achieve this output.
Other: Challenges here is Count lowercase, uppercase and number from string
https://jsgrip.blogspot.com/2020/04/javascript-challenges-count-lowercase.html

Saturday, April 25, 2020

JavaScript Challenges: Count lowercase, uppercase and number from string.

From Javascript everywhere page in facebook
Count a lowercase, uppercase, and number from string

Introduction:
    Welcome to the new javascript challenge. This problem is count Number, Lowercase(small latter), Uppercase(capital latter) from the given string.

Challenge:

   Write a logic for below input:
Input Output
Javascript Everywhere [17, 3, 0]
100k+ developer JOIN community [14, 4, 3]
what is the sum of 3445 + 5443 [14, 0, 8]
THE WORLD [0, 8, 0]

Program Description: Write a function for display count of lowercase, uppercase, and number in [<Lowercase count>, <Uppercase count>, <Number count>]

The program output is:
1)

function getCount(str) {

    var lower = str.match(/[a-z]/g);

    var upper = str.match(/[A-Z]/g);

    var number = str.match(/[0-9]/g);

    return [
        lower ? lower.length : 0,
        upper ? upper.length : 0,
        number ? number.length : 0,
     ]
}
2)

function getCount(str) {
    return [(str.match(/[a-z]/g)||[]).length,(str.match(/[A-Z]/g)||[]).length,(str.match(/[0-9]/g)||[]).length]
}

Explanation:
1) First and second both programs almost the same.
2) In the second program, we just use inline syntax and in first we explain with clear output
3) We use a pattern for solving this problem.
4) The pattern is more power full in any language we perform multiple logic using a pattern.
5) A pattern is a reusable solution that can be applied in occurring software design in our case we use in javascript problem-solving.
6) Another way pattern is a template for how we solve problems.
7) Here we use just simple patterns like [A-Z], [a-z], [0-9] inside these braces we consider as count of the length.
8) If we need just a-z count uppercase or lowercase we use a pattern like [a-zA-Z] or [\s].
9) If any confusion or query regarding pattern contacts me I will help you.

The developer just needs to write a pattern and find character and count.
There are many other solutions for this program but this is easier and fast.
This is the blog you find more challenges and other javascript information.

Other: Challenges here is count number of the vowel from string
https://jsgrip.blogspot.com/2020/04/javascript-challenge-count-vowel-number.html

Friday, April 24, 2020

JavaScript Challenge: Count a Vowel number from string

Find vowel number from string
count a vowel from string

Introduction:
    I am a Javascript developer. Now day javascript has become the most popular worldwide. But many javascript developers do his routine task daily and never try to learn more and improve his skill. So I am thinking to improve his skills and always give challenges and knowledge.

Challenge:

   Write a logic for below input:
Input Output
Javascript Everywhere 7
You are a good developer 11
Challenges 3
Welcome to the blog 6

The program output is:
1)

function getCount(str) {
    return (str.toLowerCase().match(/[aieou]/gi) || []).length;
}
2)

function getCount(str) {
    return str.length - (str.toLowerCase().replace(/[aeiou]/g, '') || []).length;
}
This is simple logic using "aeiou" pattern and you can directly identify vowel from the string.
Many developers try to use loop and indexOf that's also work but it long and more complex than the above program.

This is the blog you find more challenges and other javascript information.