Assignment

 // Q1

let arr = [75, 96, 23, 69];
let num = 71;

function larger(arr, num) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] > num) {
      console.log(arr[i]);
    }
  }
}
larger(arr, num);

// Q2
let str = "abcdacbdeefggghdh";
let ans = "";
function unique(str, ans) {
  for (let i = 0; i < str.length; i++) {
    if (ans.indexOf(str[i]) == -1) {
      ans += str[i];
    }
  }
  console.log(ans);
}
unique(str, ans);

// Q3
let country = ["Germany", "United States of America", "India", "China"];
let longest = "";
function longestCountry(country, longest) {
  for (let i = 0; i < country.length; i++) {
    if (country[i].length > longest.length) {
      longest = country[i];
    }
  }
  console.log(longest);
}
longestCountry(country, longest);

// Q4
let str2 = "an apple is a fruit";
let count = 0;
function getSum(str2, count) {
  for (let i = 0; i < str2.length; i++) {
    if (
      str2[i] == "a" ||
      str2[i] == "e" ||
      str2[i] == "i" ||
      str2[i] == "o" ||
      str2[i] == "u"
    ) {
      count++;
    }
  }
  console.log(count);
}
getSum(str2, count);

// Q5
function getRandom(start, end) {
  let diff = end - start;
  return Math.floor(Math.random() * diff) + start;
}
console.log(getRandom(3,4));

Comments

Popular posts from this blog

Callback Hell

TODO list with DOM

Refactoring callback hell code