Sharing is caring!

JavaScript exercises: For loop 3

Write a program that calculates the factorial of a number entered by the user. The factorial of n is denoted by n! and is the product of all positive integers up to and including n. For example, 5! = 5 * 4 * 3 * 2 * 1 = 120.

let num = parseInt(prompt("Enter a number:"));
let factorial = 1;

for(let i = 1; i <= num; i++) {
  factorial *= i;
}

console.log(`The factorial of ${num} is ${factorial}`);

Write a program that generates a multiplication table for the numbers from 1 to 10. The output should be in the form of a table, with the numbers from 1 to 10 as the row and column headers, and the products as the table entries.

for(let i = 1; i <= 10; i++) {
  let row = '';
  for(let j = 1; j <= 10; j++) {
    row += `${i * j}\t`;
  }
  console.log(row);
}

Write a program that generates a random integer between 1 and 100, and then prompts the user to guess the number. If the user’s guess is too low or too high, the program should provide a hint. The program should continue prompting the user until they guess the correct number.

const randomNumber = Math.floor(Math.random() * 100) + 1;
let guess;

do {
  guess = parseInt(prompt("Guess the number (between 1 and 100):"));
  
  if(guess < randomNumber) {
    console.log("Too low, try again!");
  } else if(guess > randomNumber) {
    console.log("Too high, try again!");
  }
} while(guess !== randomNumber);

console.log(`Congratulations! You guessed the number ${randomNumber}!`);

Write a program that generates the first n terms of the Fibonacci sequence. The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding numbers. The first two numbers in the sequence are 0 and 1. For example, the first 10 terms of the Fibonacci sequence are 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34.

let n = parseInt(prompt("Enter the number of terms:"));
let a = 0, b = 1;

for(let i = 1; i <= n; i++) {
  console.log(a);
  let temp = a + b;
  a = b;
  b = temp;
}

Write a program that generates a random password of length n, where n is entered by the user. The password should consist of a mix of letters, numbers, and symbols. The program should ensure that the password contains at least one uppercase letter, one lowercase letter, one number, and one symbol.

const letters = 'abcdefghijklmnopqrstuvwxyz';
const numbers = '0123456789';
const symbols = '!@#$%^&*()_+-={}[]:";\'<>?,./|\\';
let password = '';
const length = parseInt(prompt("Enter the length of the password:"));

let hasUppercase = false;
let hasLowercase = false;
let hasNumber = false;
let hasSymbol = false;

for(let i = 0; i < length; i++) {
  let type = Math.floor(Math.random() * 4);
  
  switch(type) {
    case 0:
      password += letters[Math.floor(Math.random() * letters.length)];
      hasLowercase = true;
      break;
    case 1:
      password += letters[Math.floor(Math.random() * letters.length)].toUpperCase();
      hasUppercase = true;
      break;
    case 2:
      password += numbers[Math.floor(Math.random() * numbers.length)];
      hasNumber = true;
      break;
    case 3:
      password += symbols[Math.floor(Math.random() * symbols.length)];
      hasSymbol = true;
      break;
  }
}

if(!hasLowercase) {
  password += letters[Math.floor(Math.random() * letters.length)];
}

if(!hasUppercase) {
  password += letters[Math.floor(Math.random() * letters.length)].toUpperCase();
}

if(!hasNumber) {
  password += numbers[Math.floor(Math.random() * numbers.length)];
}

if(!hasSymbol) {
  password += symbols[Math.floor(Math.random() * symbols.length)];
}

console.log(`Your random password is: ${password}`);

Write a program that prompts the user to enter a word, and then outputs all the anagrams of the word. An anagram is a word or phrase formed by rearranging the letters of another word or phrase. For example, the anagrams of “cat” are “act”, “atc”, “cat”, “cta”, “tac”, and “tca”.

function generateAnagrams(word) {
  if(word.length === 1) {
    return [word];
  }
  
  const anagrams = [];
  
  for(let i = 0; i < word.length; i++) {
    const char = word[i];
    const remainingChars = word.slice(0, i) + word.slice(i + 1);
    const subAnagrams = generateAnagrams(remainingChars);
    for(let j = 0; j < subAnagrams.length; j++) {
      anagrams.push(char + subAnagrams[j]);
    }
  }
  
  return anagrams;
}

const word = prompt("Enter a word:");
const anagrams = generateAnagrams(word);
console.log(`The anagrams of ${word} are: ${anagrams.join(", ")}`);

Write a program that prompts the user to enter a sentence, and then counts the number of words in the sentence. A word is defined as a sequence of characters separated by spaces or punctuation marks.

const sentence = prompt("Enter a sentence:");
let numWords = 0;

for(let i = 0; i < sentence.length; i++) {
  if((i === 0 || sentence[i - 1] === " ") && sentence[i] !== " ") {
    numWords++;
  }
}

console.log(`The sentence "${sentence}" contains ${numWords} words.`);

Write a program that generates a random sequence of n letters, where n is entered by the user. The sequence should contain equal numbers of vowels and consonants, and should not contain any repeated letters.

const n = Number(prompt("Enter the number of letters:"));

const vowels = ["a", "e", "i", "o", "u"];
const consonants = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"];

let letters = [];

// Generate random vowels
for(let i = 0; i < n / 2; i++) {
  let vowel = vowels[Math.floor(Math.random() * vowels.length)];
  letters.push(vowel);
}

// Generate random consonants
for(let i = 0; i < n / 2; i++) {
  let consonant = consonants[Math.floor(Math.random() * consonants.length)];
  letters.push(consonant);
}

// Shuffle letters randomly
for(let i = 0; i < letters.length; i++) {
  let j = Math.floor(Math.random() * (i + 1));
  let temp = letters[i];
  letters[i] = letters[j];
  letters[j] = temp;
}

console.log(`Random sequence of ${n} letters: ${letters.join("")}`);

JavaScript ile neler yapılabilir? JavaScript öğrenmek ne kadar sürer? JavaScript hangi program? JavaScript neden popüler?¿Qué proyectos se pueden hacer con JavaScript? ¿Qué es un proyecto en JavaScript? ¿Cómo empezar un proyecto de JavaScript? ¿Qué programas se han hecho con JavaScript? Wird JavaScript noch benötigt? Was kann man alles mit JavaScript machen? Ist JavaScript für Anfänger? Wie schwierig ist JavaScript? مشاريع جافا سكريبت للمبتدئين مشاريع جافا سكريبت جاهزة pdf مشروع جافا سكريبت javascript مشروع جافا سكريبت github تفعيل جافا سكريبت على الهاتف مشاريع جافا للمبتدئين جافا سكريبت تحميل تحميل جافا سكريبت للاندرويد

Categories: Javascript

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *