Javascript Strings Homework


Javascript Popcorn Hack

%%js
let fullName = "Tanay Paranjpe";   // <-- Add your full name

// Extract first name (characters before the space)
let firstName = fullName.substring(0,100);   // <-- Change the numbers to get the first name correctly

// Extract last name (characters after the space)
let lastName = fullName.substring(100);   // <-- Change this to get the last name correctly

// Print results
console.log("First: " + firstName);
console.log("Last: " + lastName);
<IPython.core.display.Javascript object>

Javascript Strings Homework

Task – Password Strength Checker

Write a JavaScript program that asks the user to enter a password (use prompt() in the browser).

Your program should check for these rules using string methods:

  1. The password must be at least 8 characters long.

  2. It must include the word “!” somewhere.

  3. It must not start with a space “ “.

  4. Print out one of these messages depending on the input:

  • “Strong password” if all rules are met.

  • “Weak password: too short” if less than 8 characters.

  • “Weak password: missing !” if it doesn’t include “!”.

  • “Weak password: cannot start with space” if it starts with a space.

%%js

// Example Username Checker
let username = prompt("Enter your username:");

// Rule 1: Must be at least 5 characters
if (username.length < 5) {
    console.log("Invalid username: too short");
}
// Rule 2: Cannot contain spaces
else if (username.includes(" ")) {
    console.log("Invalid username: no spaces allowed");
}
// Rule 3: Must start with a capital letter
else if (username[0] !== username[0].toUpperCase()) {
    console.log("Invalid username: must start with a capital letter");
}
// All rules passed
else {
    console.log("Valid username");
}
%%js 

// Homework starts here!