Skip to content

Instantly share code, notes, and snippets.

@imshaiknasir
Created December 22, 2024 17:49
Show Gist options
  • Save imshaiknasir/9385e0eb4963fb0544216c58e4c93a44 to your computer and use it in GitHub Desktop.
Save imshaiknasir/9385e0eb4963fb0544216c58e4c93a44 to your computer and use it in GitHub Desktop.
@imshaiknasir
Copy link
Author

factorial finding:

function factorial(num){
    let facto = 1
    for(let i=1; i<=num; i++){
        console.log(`value of 'i' is ${i}, and facto is ${facto} and after multiplying ${facto}x${i},`)
        facto *= i;
        console.log(`value of facto is: ${facto}\n`)
    }
    return facto
}

console.log(factorial(5))

@imshaiknasir
Copy link
Author

finding prime number:

let numb = prompt()

function isPrime(numb){
    if(numb>1 && numb%2 ==0){
        return "is prime"
    }else{
        return "not a prime"
    }
}

console.log(isPrime(numb))

@imshaiknasir
Copy link
Author

palindrome

let palli = prompt()
let palArr = palli.split('')
let npa = (palArr.reverse()).join('')

if (palli === npa){
    console.log("its a pallindrome")
}else{
    console.warn('no its not palli')
}

@imshaiknasir
Copy link
Author

Find the Longest Word in a String

let str = "the quick brown fox jumped over the lazy dog";

let theWord = ""
let count =0;
let newarr = []

let arrStrs= str.split(' ')
for(let i of arrStrs){
    if (i.length > count){
        count = i.length
        theWord = i
    }
}

console.log(theWord)

@imshaiknasir
Copy link
Author

convert each word's first character to uppercase

let str = "the quick brown fox jumped over the lazy dog";


let theWord = ""
let count =0;
let newarr = []

let arrStrs= str.split(' ')
for(let i of arrStrs){
    newarr = arrStrs.map(eachWord => eachWord.charAt(0).toUpperCase() + eachWord.slice(1)
    )
}
console.log(newarr.join(' '))

@imshaiknasir
Copy link
Author

/*

  1. Declare an array named 'teaFlavors' that contains
    the strings '"green tea"', '"black tea"'
    , and '"oolong tea" '
    Access the first element of the array and store it
    in a variable named 'firstTea'
    */
let str = ['green tea', 'black tea', 'oolong tea']
console.log(str[0])

/*
2. Declare an array named 'cities' containing
' "London" ' '"Tokyo"', '"Paris"' , and '"New York"',
Access the third element in the
store it in a variable named 'favoriteCi
*/

let cities = ["London", "Tokyo", "Paris", "New York"]
console.log(cities[2])

/*
3. You have an array named 'teaTypes' containing
'"herbal tea"', '"white tea"', and
• '"masala chai"'.
Change the second element of the array to '"jasmine
tea"'
*/

let teaTypes= ["herbal tea", "white tea","masala chai"]
teaTypes[1] = 'jasmine tea'
console.log(teaTypes)

/*
4. Declare an array named 'citiesVisited' containing
"Mumbai"' and '"Sydney",
Add ""Berlin"' to the array using the 'push' method.
*/

let citiesVisited =["Mumbai", "Sydney"]
citiesVisited.push('Berlin')
console.log(citiesVisited)

/*
5. You have an array named 'teaOrders' with '"chai"'
'"iced tea"', '"matcha"*
, and '"earl grey"'.
Remove the last element of the array using the 'pop'
method and store it in a variable named 'lastOrder'.
*/

let teaOrders = ["chai","iced tea", "matcha", "earl grey"]
let lastOrder = teaOrders.pop()
console.log({lastOrder});

/*
6. You have an array named "popularTeas containing
["green tea", "oolong tea", "chai"]
Create a soft copy of this array named
softCopyTeas'
*/

let popularTeas = ["green tea", "oolong tea", "chai"]
let softCopyTeas = popularTeas
console.log(softCopyTeas);

/*
7. You have an array named 'topCities' containing
'"Berlin"', '"Singapore"', and '"New York"'
Create a hard copy of this array named
'hardCopyCities'
*/

let topCities = ["Berlin", "Singapore", "New York"]
let hardCopyCities = new Array(...topCities)
console.log(hardCopyCities)
topCities.pop()
console.log(hardCopyCities)
console.log(topCities)

/*
8. You have two arrays: 'europeanCities' containing
'"Paris"' and '"Rome" •
'"Tokyo"' and '"Bangkok"'.
and '"Bangkok and asiancities containing
Merge these two arrays into a new array named
"worldCities'
*/

let europeanCities = ["Paris", "Rome"]
let asiancities =  ["Tokyo", "Bangkok"]
console.log([...europeanCities, ...asiancities])

/*
9. You have an array named 'teaMenu containing
'"masala chai"'
'"oolong tea"', '"green tea"', and
'"earl grey"'.
Find the length of the array and store it in a
variable named menuLength*.
*/

let teaMenu = ["chai","iced tea", "matcha", "earl grey"]
let menuLength = teaMenu.length
console.log({menuLength})

/*
10. You have an array named 'cityBucketList' containing
'"Kyoto"'
'"London"
, '"Cape Town"'
, and
'"Vancouver"',
Check if '"London"' is in the array and store the
result in a variable named 'isLondonInList'.
*/

let cityBucketList = ['Kyoto', 'London', 'Cape Town', 'Vancouver']
console.log(cityBucketList.includes('London'));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment