Interview Questions- Part-19

check if string input is number - using regex

				
					//To check if a string is a number or not
//VALID CASES
/*
   23-->true
  -23-->true
   23..-->false --> double decimal 
   23.23. --> false --> double decimal 
   .23 -->true
  -.23 --> true 
  sdfsfsf --> false
   23. -->true -- this should be handled as well coz --> isNaN(23.) is false
*/

function isNumeric(value) {
  let validation1 = /^(-?)((\.)\d+)$/.test(value);
  let validation2 = /^(-?)\d+(\.)?(\d?)+$/.test(value);
  if (validation1 || validation2) return true;
  return false;
}

console.log(`%c ${isNumeric("23")}`, "color:red;font-size:20px"); //true
console.log(`%c ${isNumeric("-23")}`, "color:red;font-size:20px"); //true
console.log(`%c ${isNumeric("23..")}`, "color:red;font-size:20px"); //false
console.log(`%c ${isNumeric(".23")}`, "color:red;font-size:20px"); //true
console.log(`%c ${isNumeric("-.23")}`, "color:red;font-size:20px"); //true
console.log(`%c ${isNumeric("dsdff")}`, "color:red;font-size:20px"); //false
console.log(`%c ${isNumeric("23.23.")}`, "color:red;font-size:20px"); //false
console.log(`%c ${isNumeric("23.")}`, "color:red;font-size:20px"); //true

/*explanation
validation1 --> /^(-?)((\.)\d+)$/ -->handles .232312
? -> means optional , so - is optional -->for negative values 
\d+ -> -if the number starts with decimal --> it has to be followed by digits 
+symbol near d --> means --> .3433 --> you can enter multiple digits
suppose you remove + --> then you can do only .3 --> only one digit


validation2 --> /^(-?)\d+(\.)?(\d?)+$/ --> handles  23.1212
Again ? - means optional , so - is optional -->for negative values 
Number can start with digit --can be multiple digits --> \d+
Followed by single . decimal ...it is optional --> as number can be whole number as well so optional
Then followed by digits .. I kept optional coz --> we wanted to handle 23. --> even this should be true ..thats why ? is there in \d?
At the end + --> coz if numbers exists it can be multiple ...

*/
				
			

check if string input is number - second way

				
					function isNumeric1(str) {
  if (typeof str != "string") return false;
  return !isNaN(str) && !isNaN(parseFloat(str));
}

console.log(`%c ${isNumeric1("23")}`, "color:red;font-size:20px"); //true
console.log(`%c ${isNumeric1("-23")}`, "color:red;font-size:20px"); //true
console.log(`%c ${isNumeric1("23..")}`, "color:red;font-size:20px"); //false
console.log(`%c ${isNumeric1(".23")}`, "color:red;font-size:20px"); //true
console.log(`%c ${isNumeric1("-.23")}`, "color:red;font-size:20px"); //true
console.log(`%c ${isNumeric1("dsdff")}`, "color:red;font-size:20px"); //false
console.log(`%c ${isNumeric1("23.23.")}`, "color:red;font-size:20px"); //false
console.log(`%c ${isNumeric1("23.")}`, "color:red;font-size:20px"); //true
				
			

Guess the output -

				
					const getWeekdays = (id) => {
  let weekday = "";
  switch (id) {
    case 0:
      weekday = "Sunday";
      break;
    case 1:
      weekday = "Monday";
    case 2:
      weekday = "Tuesday";
    case 3:
      weekday = "Wednesday";
      break;
    case 4:
      weekday = "Thursday";
      break;
    case 5:
      weekday = "Friday";
      break;
    case 6:
      weekday = "Saturday";
      break;
    default:
      break;
  }
  return weekday;
};

console.log(`%c ${getWeekdays(1)}`, "color:red;font-size:20px");


//Wednesday

				
			

you can wrap curly braces in switch statement...

				
					const getWeekdays = (id) => {
  switch (id) {
    case 0: {
      let weekday = "Sunday";
      console.log(weekday);
      break;
    }
    case 1: {
      let weekday = "Monday";
      console.log(weekday);
      break;
    }
    case 2: {
      let weekday = "Tuesday";
      console.log(weekday);
      break;
    }
    case 3: {
      let weekday = "Tuesday";
      console.log(weekday);
      break;
    }
    case 4:
      {
        let weekday = "Thursday";
        console.log(weekday);
        break;
      }
      break;
    case 5:
      {
        let weekday = "Friday";
        console.log(weekday);
        break;
      }
      break;
    case 6: {
      let weekday = "Saturday";
      console.log(weekday);
      break;
    }
    default: {
      let weekday = "unknown";
      console.log(weekday);
      break;
    }
  }
  return weekday;
};

console.log(`%c ${getWeekdays("1")}`, "color:red;font-size:20px");

//if you remove curly braces you will get error - variable cannot be redeclared 

				
			

Leave a Comment