others Flashcards

1
Q

Check if the given no is prime or not?

A

let number = 2
flag = false
if (number <=1){
flag = true
}

for (n=2; n<Math.sqrt(number); n++){
if(number%n ==0){
flag = true;
break;
}
}

if(flag==true){
console.log(“not prime”)}
else
{
console.log(“is prime”)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

give no. of prime nos in array

A

//Check if the given no is prime or not?
let arr = [1,4,7,8,9,11,44,39,29,19,77]
let result = []
let i =0
arr.forEach((element) =>
{
flag = false
if (element <=1){
flag = true
}

for (n=2; n<=Math.sqrt(element); n++){
if(element%n ==0){
flag = true;
break;
}

}
if (flag != true ){
// result = result.concat(element)
result.push(element)

}
}

)
console.log( “prime elements are: “+ result)
console.log(“total count: “ + result.length)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

how to take user input ?

A

?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

factorial with recursion

A

//using recursion means using functions

function factorial(number){
if(number==0){
return 1
}
let fact = number * factorial(number-1)

return fact }

console.log(factorial(5))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

factorial using for loop

A

//using recursion means using functions

function factorial(number){
let fact = 1
for(let i = 1; i<=number; i++){
fact = fact * i
}
return fact
}

console.log(factorial(5))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Generate array of Even Numbers

A

let arr= []
for(i=1; i<=20; i++){
if(i%2==0){
arr.push(i)

} } console.log(arr
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

find the largest no. in an Array(using loop )

A

// Find the largest no. in an array
let arr = [2,78,019,87,4,2,1,0,2];
let largest = arr[0];

for(let i = 1; i<arr.length; i++){
if(largest<=arr[i]){
largest = arr[i]
}

}
console.log(largest);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Count the number of vowels in a string

A

//word.includes(array pf vowels)

let string = “Vaishnavi”;
string = string.toLowerCase();
let arr = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’];
count = 0
for(let i =0 ; i<string.length; i++ ){
if (string[i] == string.contains(arr){
count = count +1
}
console.log(count)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

function that return the longest word in a sentence (17 minutes)

A

let sentence = “Helloooo! Girl you are pretty!”
sentence = sentence.split(“ “)
console.log(sentence)
maxlength = 0;
let index = 0
for(i=0; i<sentence.length; i++){
if(maxlength < sentence[i].length){
maxlength = sentence[i].length
index = i
}

}
console.log(sentence[index])

// exist another method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

To remove duplicate values

A

// to remove the duplicate values from an array
let arr = [“apple” , “banana” , “banana” , “cherry”]
let new_arr = […new Set(arr)];
console.log(new_arr)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

write a function to calculate Fibonacci (using for loop)

A

function fibo(num){
let a = 0;
if(num>=0){
console.log(a)
}
let b = 1;
if(num>=1){
console.log(b)
}

for(let i = 2; i<num; i++){

let next=a+b
console.log(next)
a=b
b=next
}
}
fibo(10)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

write a function to calculate Fibonacci (using for recursion)

A

//fibonacci using recursion
function fibonacii(num){
if(num<=0){return 0}
if(num===1){return 1}
return fibonacii(num-1)+fibonacii(num-2)
}

num = 10
for(i=0; i<num; i++){
console.log(fibonacii(i))
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

remove all whitespaces code

A

//remove all whitespaces
let string = “Vaishnavi is learning js”
let newstr =string.split(“ “).join(“”)
console.log(newstr)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

concatenate string

A

//concatinate string
let s1=”vaishnavi “
let s2=”parhad”
let s3 =s1.concat(s2)
console.log(s3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly