Untitled
unknown
plain_text
a year ago
1.4 kB
7
Indexable
let fruits = ["apple", "banana", "orange", "melon", "berry"];
console.log(fruits[2]);
fruits.push("water melon");
fruits.pop();
let person = {
name: "Sayf",
age: 16,
profession: "programmer"
};
console.log(person.age);
person.profession = "teacher";
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
Object.keys(person).forEach(key => {
console.log(key + ": " + person[key]);
});
function removeFruitByIndex(index) {
if (index >= 0 && index < fruits.length) {
fruits.splice(index, 1);
} else {
console.log("Index out of range");
}
}
function updatePersonProperty(propertyName, newValue) {
if (person.hasOwnProperty(propertyName)) {
person[propertyName] = newValue;
} else {
console.log("Property not found");
}
}
let matrix = [
[1, 2, 3],
[4, 5, 6]
];
console.log(matrix[1][2]);
matrix[0].splice(1, 1);
function printMatrix(matrix) {
for (let i = 0; i < matrix.length; i++) {
console.log(matrix[i].join(" "));
}
}
var fruitlength = fruits.map(function(fruit) {
return fruit.length;
});
function calculateAverageAge(persons) {
if (persons.length === 0) return 0;
let totalAge = 0;
for (let i = 0; i < persons.length; i++) {
totalAge += persons[i].age;
}
return totalAge / persons.length;
}
Editor is loading...
Leave a Comment