🚗 JavaScript Practice: Cars, Strings, Dates

1. Search Car with Capitalization

const arr = ['Mehran', 'Audi', 'V8', "Fx", "Foxi", "Khyber", "Alto"]; const car = prompt("Enter a car name:"); const formattedCar = car[0].toUpperCase() + car.slice(1).toLowerCase(); const check = arr.indexOf(formattedCar); if (check === -1) { output.innerText = formattedCar + " not found"; } else { output.innerText = formattedCar + " found!"; }
Result here...

2. Replace 'and' with '&'

const msg = 'ali and shafi are best friends. they play football and cricket and fight'; output.innerText = msg.replaceAll('and', '&');
Result here...

3. Convert String to Number

const str = '372'; const num = Number(str); output.innerText = "Type: " + typeof num + ", Value: " + num;
Result here...

4. Count Vowels & Consonants in "pakistan"

const str = 'pakistan'; let vowels = 0; for (let i = 0; i < str.length; i++) { if (str[i] === 'a' || str[i] === 'i') vowels++; } const consonants = str.length - vowels; output.innerText = "Vowels: " + vowels + ", Consonants: " + consonants;
Result here...

5. Type Coercion Example

let a = 5; let result = a++ + 'hello'; output.innerText = result;
Result here...

6. Logical AND Evaluation

const result = 'sahi ans' && "false" && "0" && undefined; output.innerText = result;
Result here...

7. Convert Number to String

let num = 19; let str = num.toString(); output.innerText = "Type: " + typeof str + ", Value: " + str;
Result here...

8. Replace Last Digit "5" with "6"

let num = 15; let str = num.toString(); if (str[str.length - 1] === '5') { str = str.slice(0, -1) + "6"; } output.innerText = "New value: " + str + " (" + Number(str) + ")";
Result here...

9. Show Current Time

const now = new Date().toString(); output.innerText = now.slice(16, 24); // only time
Result here...