This is a simple guessing game where a number between 1 and 100 is randomly generated and the user has to guess it. The game gives feedback whether the guessed number is too high, too low, or correct.
- Generate a random number and store it in a variable.
- Ask the user to guess a number.
- Compare the guessed number with the generated number.
- If the guessed number is higher, inform the user that their guess is too high.
- If the guessed number is lower, inform the user that their guess is too low.
- If the guessed number is equal to the generated number, congratulate the user.
- Repeat steps 2-6 until the user guesses the correct number.
Math.random
, Math.floor
, parseInt
, if/else
, alert
, prompt
This is a basic inventory system for a store. The inventory is represented as an array of objects, each with properties like "name", "price", and "quantity". Functions allow for adding, removing, or updating items in the inventory, and calculating the total value of the inventory.
- Create an empty array to store the inventory items.
- Define a function to add items to the inventory. This function should create an object with the given name, price, and quantity, and push it to the array.
- Define a function to remove items from the inventory. This function should filter the array to remove the item with the given name.
- Define a function to update items in the inventory. This function should loop through the array and update the price and quantity of the item with the given name.
- Define a function to calculate the total value of the inventory. This function should loop through the array, multiply the price and quantity of each item, and add up the results.
Array.push
, Array.filter
, for
loop, if
statement, objects
This is a basic library system where books are represented as objects. Each book has properties like "title", "author", "isAvailable", and "patron". Functions allow for checking out and returning books, and displaying the status of all books.
- Create an empty array to store the books.
- Define a function to add books to the library. This function should create an object with the given title and author, and push it to the array.
- Define a function to check out books. This function should loop through the array and update the "isAvailable" and "patron" properties of the book with the given title.
- Define a function to return books. This function should loop through the array and update the "isAvailable" and "patron" properties of the book with the given title.
- Define a function to display the status of all books. This function should loop through the array and print the properties of each book.
Array.push
, for
loop, if
statement, objects, console.log
This is a simple program that converts temperatures from Fahrenheit to Celsius and vice versa. The user inputs a temperature and a unit to convert to, and the program displays the converted temperature.
- Define a function to convert temperatures. This function should take a temperature and a unit as parameters, and return the converted temperature.
- Ask the user to input a temperature.
- Ask the user to input a unit to convert to.
- Pass the inputted temperature and unit to the conversion function and store the result.
- Display the result to the user.
parseFloat
, isNaN
, if/else
, alert
, prompt
// An object in JavaScript
let item = {
name: "Apple",
price: 1.0,
quantity: 10,
};
// Displaying an object property using alert
alert(item.name);
// Asking the user for input using prompt and storing it in a variable
let userGuess = prompt("Guess the price of the apple:");
// Converting the user's guess from a string to an integer using parseInt
let guessedPrice = parseInt(userGuess);
// Checking if the conversion was successful using isNaN
if (isNaN(guessedPrice)) {
alert("That's not a valid number!");
} else {
// Using if/else for control flow
if (guessedPrice > item.price) {
alert("Too high! Try again.");
} else if (guessedPrice < item.price) {
alert("Too low! Try again.");
} else {
alert("Congratulations! You guessed the price!");
}
}
// An array in JavaScript
let array = [];
// Adding an item to the end of the array using Array.push
array.push(item);
// Filtering the array using Array.filter
let expensiveItems = array.filter((item) => item.price > 0.5);
// Printing the expensive items
console.log(expensiveItems);
// Generating a random number between 1-10 using Math.random and Math.floor
let randomNumber = Math.floor(Math.random() * 10) + 1;
console.log(randomNumber);
// Converting a string to a float using parseFloat
let floatPrice = parseFloat("1.25");
console.log(floatPrice);
// Using a for loop to iterate over the array
for (let i = 0; i < array.length; i++) {
console.log(array[i]);
}