Javascript notes
(1). number 1 script source code
// console.log("html file is running")
// ternary operator
// let a = 4;
// (a>5)? console.log("hello") : console.log("no hello")
// no 1------------------------------------------------
// let age = prompt("enter a number")
// Number.parseInt(age);
// let age = 12;
// (age>10 && age<20)? console.log("Your age is between 10 and 20"): console.log("Your age is less than 10 or greater than 20")
//no 2-------Switch case-----------------------------
// let num = 7;
// switch (num) {
// case 1:
// console.log("this is 1")
// break;
// case 2:
// console.log("this is 2")
// break;
// case 3:
// console.log("this is 3")
// break;
// case 4:
// console.log("this is 4")
// break;
// default:
// console.log('wrong input')
// break;
// }
//no 3----------Dvisible by 2 and 3------------------------------------
// let number ;
// number = parseInt(prompt("Enter a number"));
// if(number%2==0 && number%3==0){
// console.log(`The number ${number} is divisible by both 2 and 3`);
// }
// else{
// console.log(`The number ${number} is not divisible by both 2 and 3`);
// }
//no 4----------Dvisible by 2 or 3------------------------------------
// let number ;
// number = parseInt(prompt("Enter a number"));
// if(number%2==0 || number%3==0){
// console.log(`The number ${number} is divisible by 2 or 3`);
// }
// else{
// console.log(`The number ${number} is not divisible by 2 or 3`);
// }
//no 5-------you can drive or not--------------------
// let number5 ;
// number5 = parseInt(prompt("Enter your age"));
// (number5>=18 && number5<60)? console.log("Yes, You can drive"):console.log("No, You can't drive");
(2). number 2 script source code
console.log("html file is running")
// ----------------the for...in loop-----------------------
// let obj = {
// name:"Nabin",
// age:34,
// mark:66,
// dob:"2057\/07\/06"
// }
// console.log(obj.name) this will print "Nabin"
// for(let key in obj){
// // console.log(key) //this will print only the key not value
// console.log(obj[key]) //this will now print the value
// }
// we can also use for in loops for string and array
// for(let key in arr){
// // console.log(key) //this will print only the key or iteration of value not value
// console.log(arr[key])
// }
// for(let key in string){
// //console.log(key) //this will print only the key or iteration of value not value
// console.log(string[key])
// }
// ----------------the for...of loop-----------------------
// for--of loop will work in iterable data like string and array---------
// let arr = [34,"hi",65,true,null,{name:"Nabin",age:34,mark:66,dob:"2057\/07\/06"}]
// let string ="Nabin Rimal"
// for(let key of arr){
// console.log(key) //this will print the value of array
// }
// for(let key of string){
// console.log(key) //this will print the value of string
// }
// for--of loop will not works in not iterable data like object
// for(let key of obj){
// // console.log(key) //this will print only the key not value {This will no execute}
// console.log(key)
// }
//-----------function-----------
// declaring fuunction
// function add(a,b,v){
// let sum = a+b+v;
// console.log(sum)
// }
// add(45,45,4)
//another way to declare function
// no1-----------
// let sum= (a,b,v) =>{
// return a+b+v;
// }
//console.log(sum(4,45,45)) //output
// let sum2= (a,b,v=45) =>{
// return a+b+v;
// }
// console.log(sum2(4,45)) //output
// let add = sum(45,45,4);
// console.log(sum(45,45,4)) //output
// no2-----------
// let addition= () =>
// console.log("hello")
// addition()
//---------no1 ------------------------------
// let obj = {
// name:"nabin",
// age:23,
// height:5.8
// }
// console.log(Object.keys(obj)) //returns the keys of object
// console.log( Object.keys(obj).length) //returns the no. of keys of object
// for(let i =0;i<Object.keys(obj).length;i++){
// // console.log(Object.keys(obj)[i]) //prints only keys
// // console.log(obj[Object.keys(obj)[i]]) //prints values
// }
//---------no2 ------------------------------
// using for--in
// for(let key in obj){
// console.log(obj[key])
// }
//---------no3 ------------------------------
// let num = parseInt(prompt("Enter a number"));
// let random = Math.round(Math.random()*100);
// console.log(random)
// let num
// while(num!=random){
// console.log("Try again")
// num = parseInt(prompt("Enter a number"));
// }
// console.log("Correct number")
//---------no4 ------------------------------
// const avg = (a,b,v,d,s) => {
// return a+b+v+d+s;
// }
// console.log(avg(4,6,65,6,65))
(3). number 3 script source code
console.log("html file is running well")
// template leterals
// let Name = "Bipin Rimal"
// let age = 15
// let cls = 9
// let weight = 52
// let persondetail = ["Bipin Rimal",15,9,52]
// console.log(`My name is ${persondetail[0]}. I am ${persondetail[1]} years old. I study in class ${persondetail[2]}. My weight is ${persondetail[3]}.`)
//Escape sequenec character------------------------------
// console.table("My name is bipin rimal. My Mother\'s name is . she said \" i am a good person \"")
// console.log('hello\th')
// console.log('hello\rh')
///------------------string properties and methods------------------
// let fname = "Nabin"
// let lname = "Rimal"
// let str = "hello I am nabin rimal"
//------------length of array---------------
// console.log(fname.length,lname.length)
//--------------uppercase----------
// console.log(fname.toUpperCase(),lname.toUpperCase())
//-------------lowercase-------
// console.log(fname.toLowerCase(),lname.toLowerCase())
//-------slice-------
// console.log(str.slice(3,9))
// console.log(str.slice(11,22))
//-----------replaces--------
//creates a new string named newstr
// let newstr = str.replace("nabin","prabin")
// console.log(newstr)
// console.log(str.replace("nabin","prabin"))
//-------------concat-----
// console.log(fname.concat(" ",lname))
//----------------------trim------------------
// let strings = " Hello I am nabin rimal"
// let tri = strings.trim()
// console.log(tri)
// ---------------question no 1--------------------
// console.log("Nabin\"".length)
// ---------------question no 2--------------------
// let strings2 = "I am nabin rimal"
// -----------includes-----
// console.log(strings2.includes("nabin")) //"nabin" is in the strings2 so prints true
// console.log(strings2.includes("hi")) //"hi" is not in the strings2 so prints false
// ---------ends with---------
// console.log(strings2.endsWith("Rimal")) //outputs false
// console.log(strings2.endsWith("rimal")) //outputs true
// ----------start with ------------
// console.log(strings2.startsWith("i")) //outputs false
// console.log(strings2.startsWith("I")) //outputs true
// ---------------question no 3--------------------
// let strings3 = "I am NABIN Rimal"
// console.log(strings3.toLowerCase())
// ---------------question no 4--------------------
// let strings4 = "Please give Rs 1000"
// let num =parseInt(strings4.slice(15,19))
// console.log(typeof num,`=>>Take your ${num} Rs`)
// ---------------question no 5--------------------
// you can not change existing string any more with re initialize
// let strings3 = "I am NABIN Rimal"
// strings3[5] = "P"
// strings3[6] = "R"
// strings3[7] = "A"
// strings3[8] = "B"
// strings3[9] = "I"
// strings3[10] = "N"
// console.log(strings3)
(4). number 4 script source code
console.log("html file is running well")
//--------length of array---------
let arr = ["nabin", 22, true, 5 == 2]
// console.log(arr)
// let lengthofarr = arr.length
// console.log(lengthofarr)
//------arrays are mutable.arrays can be change-----------
// arr[0]="Nabin"
// console.log(arr,arr[0],typeof arr) //type of arr is an object in JS
//--------------------- Array Methods ------------
//------- Method ----------tostring()------
//-----convert to string separated with comma----
// console.log(arr.toString())
//------- Method 2----------join()------
//-------joins elements of array using a separator----
// let uparrow = arr.join('^')
// let underscore = arr.join("_")
// console.log(uparrow)
// console.log(underscore)
// console.log(arr)
//------- Method 3----------pop()------
//---remove last element from the array---
// let popedvalue = arr.pop()
// console.log(popedvalue) //returns the poped value
// console.log(arr) //+++++++++it updates the array+++++++++++
//------- Method 4----------shift()------
//-----remove first element from the array
// let shifted_value = arr.shift()
// console.log(shifted_value) //returns the shifted value
// console.log(arr) //+++++++++it updates the array+++++++++++
//------- Method 5----------push()------
//----add the new element at the end
// let pushed = arr.push("Rimal",55)
// console.log(pushed) //return the length of that array after pushed
// console.log(arr) //+++++++++it updates the array+++++++++++
//------- Method 6----------unShift()------
//----add the new element at the begining
// let unshifted = arr.unshift("Hello")
// console.log(unshifted) //return the length of that array after unshifted
// console.log(arr) //+++++++++it updates the array+++++++++++
//----Method 7----delete (it is an operator like typeof not function)-------
//--------delete the element of a specific index----------
// delete arr[3]
// console.log(arr) //deleted value shows empty
//----Method 8----concat()-------
//------------It is used to join arrays---------------
let arr2 = [34, 3, 5, 55, 23, 31]
// let concated_array = arr.concat(arr2,"concat joins two arrays")
// console.log(concated_array)
//----Method 8----sort()-------
//----------sort an array alphabetically---------
// let sorted = arr2.sort()
// console.log(sorted) // output the sorted array
//sort function sort value alphabetically. start form 1 and 2, 3,4 so on ===like [78,55,6,5,46,8] to [46,5,55,6,78,8]
//------it also updates the array---
// console.log(arr2)
// ----------sorting the number right way-----
// let compare_array = (num1, num2) => {
// // return num1-num2; //accending order
// // return num2-num1; //decending order
// }
// console.log(arr2.sort(compare_array)) //sorts the array
//----Method 9----splice()-------
//it is used to add new items to array
// it's sentence is
//arr.splice(position to add,no of element to remove,elements to add)
// let spliced_array = arr2.splice(1, 5, 44,54,64,74,84)
// console.log(spliced_array,"these are removed elements") //returns removed elements
// console.log(arr2) //---------it also changes exsting array---------
//---------Method 9----slice()---------
//slice the array into pieces
//arr2.slice(no of index starts from , no of index ends on but without including it )
// let sliced1 = arr2.slice(2)
// let sliced2 = arr2.slice(2,4)
// console.log(arr2,sliced1,sliced2) //it returns the sliced array and doesn't change the array
//---------Method 10----reverese()---------
// let reversed_array = arr2.reverse() //it will reverse and updates the array
// console.log(reversed_array,arr2)
// ---------------------Looping through array---------------
let arr3 = [1, 2, 3, 4, 5];
// (1)-----------foreach loop ----------------------
// let sum = 0;
// let narr = arr3.forEach((v) => {
// return sum = v + sum; //calls function for each element of arr2 and execute the fuction logic
// })
// console.log(sum)
// (2)-----------for.....of loop ----------------------
///for of loop only runs with iterable data types like string,array
// for(let key of arr3){
// console.log(key)
// }
// (3)-----------for.....in loop ----------------------
// for(let key in arr3){
// console.log(arr3[key])
// }
// (4)-----------array from loop ----------------------
let str2 = "Nabin_Rimal"
// console.log(typeof str2)
// let strToarray = Array.from(str2)
// console.log(strToarray, typeof strToarray, str2) // it does not update existing string
// (5)----------- Map----------------------
// it runs as same as foreach
// let b = 0;
// arr2.map((element) => {
// console.log(arr2[b], element + element)
// b += 1;
// })
// it will creates an array or returns an array
// let maped = arr3.map((element) => {
// return element * element
// })
// console.log(maped, arr3)
// (6)----------- Reduce----------------------
// reduce the array to a single value
// let reduced = arr3.reduce((k, h) => {
// return (k + h)
// })
// console.log(reduced, arr3)\
// (7)----------- filter----------------------
//filter an array with a condition and creates new array
// let filter_array =arr3.filter((value) =>{
// return value<3
// })
// console.log(filter_array,arr3)
// ----------------question 1------------
// let userinput = parseInt(prompt("Enter a Number"))
let newarr = [1, 2, 3];
// newarr.push(userinput);
// console.log(newarr)
// ----------------question 2------------
// let userinput;
// console.log(typeof userinput)
// -------------while loop---------
// while(userinput!=0){
// userinput = parseInt(prompt("Enter a Number"))
// newarr.push(userinput);
// }
// -------do while loop---------
// do{
// userinput = parseInt(prompt("Enter a Number"))
// newarr.push(userinput);
// }while(userinput!=0)
// ------for loop---------
// for(;userinput!==0;){
// userinput = parseInt(prompt("Enter a Number"))
// newarr.push(userinput)
// }
// console.log(newarr)
// ----------------question 3------------
// let arr6 = [55,6,1,21,21,10,152,410,1,50,51,0,08584,0,20200]
// let filtered = arr6.filter((arr6_value) => {
// return arr6_value%10==0
// })
// console.log(filtered)
// ----------------question 4------------
// let arr7 =[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50]
// let square_array = arr7.map((value)=>{
// return value*value
// })
// console.log(square_array)
// ----------------question 5------------
// input = parseInt(prompt("Enter the nth Natural number"))
// let factorial;
// if(input==0){
// factorial=1;
// }
// else{
// let blank_arr =[];
// for(let i=1;i<=input;i++){
// blank_arr.push(i);
// }
// console.log(blank_arr)
// factorial = blank_arr.reduce((val1,val2)=>{
// return val1*val2
// })
// }
// console.log(`the factorial of ${input} is ${factorial}`)
(5). number 5 HTML source code