NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

https://www.softwaretestinghelp.com/coding-interview-questions/
extra --> https://www.programiz.com/javascript/examples/check-occurrence-string

-->Reverse a String With Built-In Functions
1st method
function reverseString(str){
var splitString = str.split("");
console.log("splitString",splitString)

var reverseString = splitString.reverse();
console.log(reverseString,"reverseString")

var joinString = reverseString.join("");
console.log(joinString,"joinString")

return joinString;
}

var a = reverseString("hello! how are you");
console.log(a,'ok')

2nd method :
function reverseString(str){
var splitString = str.split("").reverse().join("");

return splitString;
}
var a = reverseString("hello! how are you");
console.log(a,'ok')

--> Reverse a String With a Decrementing For Loop
function reverseString(str) {
// Step 1. Create an empty string that will host the new created string
var newString = "";

// Step 2. Create the FOR loop
/* The starting point of the loop will be (str.length - 1) which corresponds to the
last character of the string, "o"
As long as i is greater than or equals 0, the loop will go on
We decrement i after each iteration */
for (var i = str.length - 1; i >= 0; i--) {
newString += str[i]; // or newString = newString + str[i];
}
/* Here hello's length equals 5
For each iteration: i = str.length - 1 and newString = newString + str[i]
First iteration: i = 5 - 1 = 4, newString = "" + "o" = "o"
Second iteration: i = 4 - 1 = 3, newString = "o" + "l" = "ol"
Third iteration: i = 3 - 1 = 2, newString = "ol" + "l" = "oll"
Fourth iteration: i = 2 - 1 = 1, newString = "oll" + "e" = "olle"
Fifth iteration: i = 1 - 1 = 0, newString = "olle" + "h" = "olleh"
End of the FOR Loop*/

// Step 3. Return the reversed string
return newString; // "olleh"
}

reverseString('hello');

--> Reverse a String With Recursion
function reverseString(str) {
return (str === '' " ) ? " '' : reverseString(str.substr(1)) + str.charAt(0);
/*
First Part of the recursion method
You need to remember that you won’t have just one call, you’ll have several nested calls

Each call: str === "?" reverseString(str.subst(1)) + str.charAt(0)
1st call – reverseString("Hello") will return reverseString("ello") + "h"
2nd call – reverseString("ello") will return reverseString("llo") + "e"
3rd call – reverseString("llo") will return reverseString("lo") + "l"
4th call – reverseString("lo") will return reverseString("o") + "l"
5th call – reverseString("o") will return reverseString("") + "o"

Second part of the recursion method
The method hits the if condition and the most highly nested call returns immediately

5th call will return reverseString("") + "o" = "o"
4th call will return reverseString("o") + "l" = "o" + "l"
3rd call will return reverseString("lo") + "l" = "o" + "l" + "l"
2nd call will return reverserString("llo") + "e" = "o" + "l" + "l" + "e"
1st call will return reverserString("ello") + "h" = "o" + "l" + "l" + "e" + "h"
*/
}
reverseString("hello");

--> Check Palindrome using built-in Functions
function reverseString(str) {
var checkPalindrome = str.split("").reverse().join("");
if(checkPalindrome === str){
console.log("it is a plaindrome")
return checkPalindrome ;
}
else{
console.log("it is not plaindrome")
return str ;
}
}
var a = reverseString(prompt('Enter a string: '));
console.log(a)

--> Check Palindrome Using for Loop
1st method : reverse a string then match if both old and new string matches or not
function reverseString(str) {

var newString = "";

for(var i = str.length-1 ; i>=0 ; i--){
newString += str[i]
}

var b = newString === str ? (newString + " is a plaindrome") : (newString + " is not plaindrome");
return b;

}
var a = reverseString("madam");
console.log(a)

2nd method - check and match inside for loop if first letter matches last letter ot not
function checkPalindrome(str) {

// find the length of a string
const len = string.length;

// loop through half of the string
for (let i = 0; i < len / 2; i++) {

// check if first and last string are same
if (string[i] !== string[len - 1 - i]) {
return 'It is not a palindrome';
}
}
return 'It is a palindrome';
}

// take input
const string = prompt('Enter a string: ');

// call the function
const value = checkPalindrome(string);

console.log(value);

--> find number of matching characters

var b = (("hello, i am learning javascript".match(new RegExp("a", "g")) || []).length);
console.log(b)

--> Count the Number of Vowels Using Regex
function noOfVowels(str){
const count = str.match(/[aeiou]/gi).length;
return count;
}
var a = noOfVowels("Hello World");
console.log(a);

-->Count the Number of Vowels Using for Loop
function noOfVowels(str){
let count = "";
for (let letter of str.toLowerCase()){
if(vowels.includes(letter)){
count++;
}
}
return count;
}
var vowels = ["a","e","i","o","u"]
var a = noOfVowels("NO of vowels");
console.log(a);

--> calculate the number of vowels and consonants in a string in javascript
function noOfVowels(str){
let count = "";
let consonants = "";

for (let letter of str.toLowerCase()){
if(vowels.includes(letter)){
count++;

}
else{
consonants ++
}
}
console.log(count,"count");
console.log(consonants,"consonants");
return count;

}

var vowels = ["a","e","i","o","u"]
var a = noOfVowels("NO of vowels");
console.log(a);

--> How do you prove that the two strings are anagrams?
function checkAnagrams(a, b){
let lengthofa = a.length;
let lengthofb = b.length;

if(lengthofa !== lengthofb){
console.log("invalid")
}

var checka = a.split("").sort().join("");
var checkb = b.split("").sort().join("");

if(checka ===checkb){
return true
}
else{
return false
}
}

let a = checkAnagrams('hola','aloh')
console.log(a,'a')

--> Check Occurrence of a Character Using for Loop
function countString(str, letter) {
let count = 0;

// looping through the items
for (let i = 0; i < str.length; i++) {

// check if the character is at that position
if (str.charAt(i) == letter) {
count += 1;
}
}
return count;
}

// take input from the user
const string = prompt('Enter a string: ');
const letterToCheck = prompt('Enter a letter to check: ');

//passing parameters and calling the function
const result = countString(string, letterToCheck);

// displaying the result
console.log(result);

--> Check occurrence of a character using a Regex
function findChar (str,letter){
var re = new RegExp(letter,'g');
var count = str.match(re).length;
return count;
}
const stringToCheck = "Hello world";
const letterToCount = "l";
let result = findChar(stringToCheck, letterToCount)
console.log(result,'result')

--> How to verify if a number is prime or not in javascript
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.