Virtual Personal Assistant JavaScript, AI

👤 Sharing: AI
```javascript
// Virtual Personal Assistant using JavaScript and basic AI principles

class VirtualAssistant {
  constructor() {
    this.name = "Assistant";
    this.knowledgeBase = {
      "what is the capital of france": "Paris is the capital of France.",
      "what is the meaning of life": "The meaning of life is subjective and varies from person to person.",
      "what is javascript": "JavaScript is a high-level, interpreted programming language.",
      "what is your name": "My name is " + this.name + ".",
      "how are you": "I am doing well, thank you for asking."
    };
    this.user = null; // Track the user's name
  }

  greetUser() {
    return "Hello! How can I help you today?";
  }

  setUser(name) {
    this.user = name;
    return "Nice to meet you, " + name + "!";
  }

  respondTo(userInput) {
    userInput = userInput.toLowerCase(); // Make the input case-insensitive

    // Personalized Greetings
    if (userInput.includes("hello") || userInput.includes("hi")) {
      if (this.user) {
        return "Hello " + this.user + "!";
      } else {
        return "Hello! What's your name?";
      }
    }

    // Check for user name setting
    if (userInput.includes("my name is") || userInput.includes("i am")) {
      const name = userInput.split("is ")[1] || userInput.split("am ")[1]; // Simple name extraction
      if (name) {
        return this.setUser(name);
      } else {
        return "Sorry, I couldn't understand your name. Could you please tell me again?";
      }
    }

    // Knowledge Base Lookup
    if (this.knowledgeBase[userInput]) {
      return this.knowledgeBase[userInput];
    }

    // Simple keyword-based responses (basic AI)
    if (userInput.includes("weather")) {
      return "I'm sorry, I can't provide real-time weather information yet.  Check a weather website or app.";
    }

    if (userInput.includes("time")) {
      const now = new Date();
      const timeString = now.toLocaleTimeString();
      return "The current time is " + timeString;
    }

    if (userInput.includes("help")) {
        return "I can answer simple questions and tell the time. Try asking me 'What is the capital of France?' or 'What is the time?'";
    }

    // Default response if no match is found
    return "I'm sorry, I don't understand.  Could you please rephrase your question?  Or try asking me something else. Try asking me 'help' to know what i can do";
  }
}


// Example Usage (in a browser console or Node.js environment)
const assistant = new VirtualAssistant();
console.log(assistant.greetUser());

// Simulate user input and output
function simulateUserInput(userInput) {
    console.log("User: " + userInput);
    const response = assistant.respondTo(userInput);
    console.log("Assistant: " + response);
    return response; // Allow for testing in Node.js or other environments
}

// Example interactions:
simulateUserInput("Hello");
simulateUserInput("My name is John");
simulateUserInput("what is javascript");
simulateUserInput("What is the time?");
simulateUserInput("What is the weather?");
simulateUserInput("What is the capital of france");
simulateUserInput("help");
simulateUserInput("Tell me a joke"); // Will get the default response.

// For use in a web page:

// Assuming you have an input field with id="userInput" and a display area with id="output"

/*
document.getElementById("userInput").addEventListener("keyup", function(event) {
  if (event.key === "Enter") {
    const userInput = document.getElementById("userInput").value;
    const response = assistant.respondTo(userInput);
    document.getElementById("output").textContent = "Assistant: " + response;
    document.getElementById("userInput").value = ""; // Clear the input
  }
});
*/


//  Node.js example to run it on your terminal
/*
const readline = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout,
});

function askQuestion() {
  readline.question('You: ', (userInput) => {
    const response = assistant.respondTo(userInput);
    console.log('Assistant: ' + response);

    if (userInput.toLowerCase() !== 'exit') { // Allows the user to exit the interaction
      askQuestion(); // Recursive call for continuous interaction
    } else {
      readline.close(); // Closes the readline interface upon exiting
    }
  });
}

console.log(assistant.greetUser());
askQuestion();

*/
```
👁️ Viewed: 10

Comments