0% found this document useful (0 votes)
11 views

Js Bedelliyeni

The document contains code snippets demonstrating various JavaScript array and string methods. It includes examples of: 1. Summing array elements using forEach or for loops 2. Checking if a number is even or odd using modulo 3. Calculating factorials recursively 4. Changing HTML elements with DOM methods 5. Checking palindromes with reverse and join 6. Mapping, filtering, reducing arrays 7. Capitalizing first letters with slice and toUpperCase 8. Counting vowels with includes 9. Dynamically changing styles with user input

Uploaded by

nurayjumayeva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Js Bedelliyeni

The document contains code snippets demonstrating various JavaScript array and string methods. It includes examples of: 1. Summing array elements using forEach or for loops 2. Checking if a number is even or odd using modulo 3. Calculating factorials recursively 4. Changing HTML elements with DOM methods 5. Checking palindromes with reverse and join 6. Mapping, filtering, reducing arrays 7. Capitalizing first letters with slice and toUpperCase 8. Counting vowels with includes 9. Dynamically changing styles with user input

Uploaded by

nurayjumayeva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

YA HAK

4. Calculate the sum of all numbers in an array. Provide an


example with an array of numbers.

const arr = [1,2,3,4,5];


let sum = 0;
arr.forEach((currentnum)=>{
sum+=currentnum;
}
)
console.log(sum);

Or
const arr = [1,2,3,4,5];
let sum=0;
for(let i=0;i<arr.length;i++) sum+=arr[i];
console.log(sum);

6. Create a JavaScript function that gets a number as a


parameter and then displays whether the number is even or
odd.

function checker(num){
if(num%2===0) return "even";
else return "odd";
}
console.log(checker(18));

8. Write a JavaScript function to calculate and return the


factorial of a given number.
function checker(num){
if(num===0) return 1;
if(num===1) return num;
else return num * checker(num-1);
}
console.log(checker(5));

12.
const head = document.getElementById("myHeading");
const txt = document.getElementsByTagName("p")[0];

document.body.style.backgroundColor = "green"; //a


head.textContent = "Hello JS!"; //b
txt.classList.remove("inner-text"); //c
txt.style.fontSize = "30px"; //d

13. Write a JavaScript function that checks whether a string


is a palindrome.
function checker(s) {
s1 = s.split("").reverse().join("");
if (s === s1) console.log("Yes");
else console.log("No");
}

checker("qarşida");
checker("radar");
checker("var");
19. const numbers = [12, 23, 45, 67, 34, 73];
a) Create a new array that contains the square of each number in
the numbers array. (4 points)

const numbers = [12, 23, 45, 67, 34, 73];


let square = numbers.map((x) => {
return x * x;
});
console.log(square);
Or
const numbers = [12, 23, 45, 67, 34, 73];
let newarr=[];
for(let i = 0;i < numbers.length;i++){
newarr.push(numbers[i] * numbers[i]);
}
console.log(newarr);

b) Create a new array that contains only the even numbers from
the numbers array. (4 points)

const numbers = [12, 23, 45, 67, 34, 73];


const evenarr = numbers.filter((x) => {
return x % 2 === 0;
});
console.log(evenarr);
Or
const numbers = [12, 23, 45, 67, 34, 73];
let evenarr=[]
for(let i = 0;i < numbers.length;i++){
if(numbers[i]%2===0) evenarr.push(numbers[i]);
}
console.log(evenarr);

c) Find the sum of all the numbers in the numbers array. (4 points)

const numbers = [12, 23, 45, 67, 34, 73];


let sum = 0;
numbers.forEach((x) => {
sum += x;
});
console.log(sum);
Or
const numbers = [12, 23, 45, 67, 34, 73];
let sum = 0;
for(let i = 0;i < numbers.length;i++){
sum+=numbers[i];
}
console.log(sum);

d) Sort the numbers array in ascending order. (4 points)

const numbers = [12, 23, 45, 67, 34, 73];


numbers.sort(function(a, b){return a - b});
console.log(numbers);

e) Use the forEach() method to log each number in the numbers


array to the console. (4 points)
const numbers = [12, 23, 45, 67, 34, 73];
numbers.forEach((x) => {
console.log(x);
});
Or
const numbers = [12, 23, 45, 67, 34, 73];
numbers.forEach(function(x){
console.log(x);
})

24. Write a JavaScript function that gets a month (as a number


from 1 to 12) and then displays the number of days in that
month using swithc/case.
function getDaysInMonth(month) {
switch (month) {
case 1: // January
case 3: // March
case 5: // May
case 7: // July
case 8: // August
case 10: // October
case 12: // December
return 31;
case 4: // April
case 6: // June
case 9: // September
case 11: // November
return 30;
case 2: // February
return 28;
default:
return "Invalid month";
}
}

console.log(getDaysInMonth(3));
console.log(getDaysInMonth(8));
console.log(getDaysInMonth(2));
console.log(getDaysInMonth(13));
27. Write a JavaScript function to find the largest number in an
array.
function findbigger(nums) {
nums.sort(function(a,b){return a - b});
console.log(nums[nums.length - 1]);
}
findbigger([1, 7, 4, 6, 5]);

Or
function findbigger(nums) {
let max = nums[0];
for(let i = 1;i < nums.length;i++){
if(nums[i]>max) max = nums[i];
}
console.log(max);
}
findbigger([1, 7, 4, 6, 5]);

31.Write a JavaScript program that uses a for loop to output to the


console only even integers from 1 to 100.
for (let i = 1; i <= 50; i++){
console.log(i*2);
}

33.Write a JavaScript function that takes an object as a parameter


and prints all of its properties and values.
function printObjectProperties(obj) {
for (let key in obj) {

console.log(`${key}: ${obj[key]}`);

}
}
const myObject = {
surname: "Bedelli",
age: 30,
city: "Ismayilli-Basarkecher",
};
printObjectProperties(myObject);
Or
function printObjectProperties(obj) {
for (let key in obj) {
console.log(key + ": " + obj[key]);
}
}
const myObject = {
name: "Bedelli",
age: 30,
city: "Ismayilli-Basarkecher",
};
printObjectProperties(myObject);

36. Write a JavaScript function to reverse the characters in a


string.
function toreverse(s) {
s = s.split("").reverse().join("");
console.log(s);
}
toreverse("bedelli");

37. const myArray = [...]


a) Add the element &#39;fish&#39; to the end of the nested array
(4 points)

const myarray = ["John", 30, true, ["dog", "cat"]];


const pets = myarray[3];
pets.push("fish");
console.log(myarray);

b) Remove the first element from the main array. (4 points)

const myarray = ["John", 30, true, ["dog", "cat"]];


myarray.shift();// unshift elave edir,shift silir.
console.log(myarray);

c) Add the element &#39;Jane&#39; to the beginning of the main


array. (4 points)

const myarray = ["John", 30, true, ["dog", "cat"]];


myarray.unshift("Jane");
console.log(myarray);

d) Remove the second element from the main array and replace it
with &#39;36&#39;. (4 points).
const myarray = ["John", 30, true, ["dog", "cat"]];
myarray[1] = 36;
console.log(myarray);

e) Create a new array that combines the nested array with


another array [&#39;bird&#39;, &#39;hamster&#39;]. (4 points)
const myarray = ["John", 30, true, ["dog", "cat"]];
const pet1 = myarray[3];
const pet2 = ["bird", " hamham"];
const pets = pet1.concat(pet2);
console.log(pets);
Or
const myarray = ["John", 30, true, ["dog", "cat"]];
let pet1 = myarray[3];
const pet2 = ["bird", " hamham"];
for(let i=0;i < pet2.length;i++){
pet1.push(pet2[i]);
}
console.log(pet1);

38. Write a JavaScript function that counts the number of vowels


in a given string.
function countVowels(str) {
const wowels = "aeiouAEIOU";
let count = 0;
for (let i = 0; i < str.length; i++) {
if (wowels.includes(str[i])) count++;
}
console.log(count);
}
countVowels("kesilessen qAqam");
40. Develop a JavaScript program that changes an HTML
element’s opacity from 0 to 1 when click a button.
(html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"
/>
<title>Document</title>
<style>
#myElement {
width: 200px;
height: 200px;
background-color: blue;
opacity: 0;
transition: opacity 1s ease-in-out;
}
</style>
</head>
<body>
<div id="myElement"></div>
<button class="btn">click</button>
<script src="index.js"></script>
</body>
</html>

(js)
const btn = document.querySelector(".btn");
const block = document.querySelector("#myElement");
let count = 0;
btn.addEventListener("click", () => {
if (block.style.opacity === "0") block.style.opacity = "1";
else block.style.opacity = "0";
});

OR
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"
/>
<title>Document</title>
<style>
#myElement {
width: 200px;
height: 200px;
background-color: blue;
opacity: 0;
}
</style>
</head>
<body>
<div id="myElement"></div>
<button class="btn" onclick="clicker">click</button>
<script src="index.js">
function clicker(){
const btn = document.querySelector(".btn");
const block = document.querySelector("#myElement");
if (block.style.opacity === "0") block.style.opacity = "1";
else block.style.opacity = "0";
}
</script>
</body>
</html>
45. Develop a JavaScript function that takes an array of strings
and returns a new array with each string&#39;s first letter
capitalized.

function letterCapitalized(s) {
let news = s.map((x) => {
return x[0].toUpperCase() + x.slice(1);
});
console.log(news);
}
letterCapitalized(["dostum", "murebbe", "isteyirsen?"]);

Or
function letterCapitalized(s) {
let newarr = [];
for (let i = 0; i < s.length; i++) {
newarr.push(s[i][0].toUpperCase() + s[i].slice(1));
}
console.log(newarr);
}
letterCapitalized(["dostum", "murebbe", "isteyirsen?"]);

47. Write a JavaScript function to dynamically change the


background color of a web page based on user input.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"
/>
<title>Document</title>
</head>
<body>
<input class="inp" type="text" name="" id="" />
<button class="btn">click</button>
<script src="index.js"></script>
</body>
</html>
const btn = document.querySelector(".btn");
const inp = document.querySelector(".inp");
btn.addEventListener("click", () => {
let val = inp.value;
document.body.style.backgroundColor = val;
});

Or
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"
/>
<title>Document</title>
</head>
<body>
<input class="inp" type="text" name="" id="" />
<button class="btn" onclick="colorchanger">click</button>
<script src="index.js">
const btn = document.querySelector(".btn");
const inp = document.querySelector(".inp");
function colorchanger(){
let val = inp.value;
document.body.style.backgroundColor = val;
}
</script>
</body>
</html>

50. Develop a JavaScript function that takes an array of strings


and returns a new array with each string&#39;s first letter
capitalized.(tekrar sualdi);

You might also like