Perulangan

Pre Bootcamp
mail@pastecode.io avatar
unknown
javascript
3 years ago
3.4 kB
5
Indexable
Never
function reverseString(str) {
  var newString = "";

  for (var i = str.length - 1; i >= 0; i--) {
    newString += str[i]; // or newString = newString + str[i];
  }

  /* Here hello's length equals 5
        For each iteration: i = str.length - 1 and newString = newString + str[i]
        First iteration:    i = 5 - 1 = 4,         newString = "" + "o" = "o"
        Second iteration:   i = 4 - 1 = 3,         newString = "o" + "l" = "ol"
        Third iteration:    i = 3 - 1 = 2,         newString = "ol" + "l" = "oll"
        Fourth iteration:   i = 2 - 1 = 1,         newString = "oll" + "e" = "olle"
        Fifth iteration:    i = 1 - 1 = 0,         newString = "olle" + "h" = "olleh"
    End of the FOR Loop*/

  return newString; // "olleh"
}

reverseString("hello");

function reverseString(str) {
  // Step 1. Use the split() method to return a new array
  var splitString = str.split(""); // var splitString = "hello".split("");
  // ["h", "e", "l", "l", "o"]

  // Step 2. Use the reverse() method to reverse the new created array
  var reverseArray = splitString.reverse(); // var reverseArray = ["h", "e", "l", "l", "o"].reverse();
  // ["o", "l", "l", "e", "h"]

  // Step 3. Use the join() method to join all elements of the array into a string
  var joinArray = reverseArray.join(""); // var joinArray = ["o", "l", "l", "e", "h"].join("");
  // "olleh"

  //Step 4. Return the reversed string
  return joinArray; // "olleh"
}

reverseString("hello");

const deret = (n) => {
  let deretAngka = [];
  for (i = 1; i <= n; i++) {
    deretAngka.push(i);
  }
  console.log(deretAngka);
};

deret(15);

const deretKuadrat = (n) => {
  let deretAngka = [];
  for (i = 1; i <= n; i++) {
    deretAngka.push(i * i);
  }
  console.log(deretAngka);
};

deretKuadrat(15);

const deretKelipatanTiga = (n) => {
  let deretAngka = [];
  for (i = 1; i <= n; i++) {
    let temp = i;
    temp = temp * 3;
    deretAngka.push(temp);
  }
  console.log(deretAngka);
};

deretKelipatanTiga(20);

// program to check if a number is prime or not

// take input from the user
const checkNumberIsPrime = (number) => {
  let isPrime = true;

  // check if number is equal to 1
  if (number === 1) {
    console.log("1 is neither prime nor composite number.");
  }

  //prime number bilangan prima

  // check if number is greater than 1
  else if (number > 1) {
    // looping through 2 to number-1
    for (let i = 2; i < number; i++) {
      if (number % i == 0) {
        isPrime = false;
        break;
      }
    }

    if (isPrime) {
      console.log(`${number} is a prime number`);
    } else {
      console.log(`${number} is a not prime number`);
    }
  }

  // check if number is less than 1
  else {
    console.log("The number is not a prime number.");
  }
};

checkNumberIsPrime(10);

//odd or even ganjil genap

const oddOrEven = () => {
  // program to check if the number is even or odd
  // take input from the user
  const number = prompt("Enter a number: ");

  //check if the number is even
  if (number % 2 == 0) {
    console.log("The number is even.");
  }

  // if the number is odd
  else {
    console.log("The number is odd.");
  }
};

oddOrEven(2);

//hitung word

const WordCount = (str) => {
  return str.split(" ").filter((n) => {
    return n != "";
  }).length;
};