Skip to content

Instantly share code, notes, and snippets.

@vladislavaSim
Last active November 22, 2021 20:14
Show Gist options
  • Save vladislavaSim/7af149293818a3bb20120d590b3d40e4 to your computer and use it in GitHub Desktop.
Save vladislavaSim/7af149293818a3bb20120d590b3d40e4 to your computer and use it in GitHub Desktop.
Home_training_22112021
// showing object info
let dog = {
name: 'Ronnie',
breed: 'chihuahua',
weight: 2.1,
color: 'white-red',
age: 5
};
for(let feature in dog) {
let info = feature + dog[feature];
}
//iterate array until 0 found
let nums = [6, -54, 0, 33, -5];
for(let i = 0; i < nums.length; i++) {
if(nums[i] !== 0) {
console.log(nums[i])
} else {
break
}
}
//show summary from all positive numbers from array
let nums = [6, -54, 4, 33, -5];
let sumNums = 0;
for(let i = 0; i < nums.length; i++) {
if(nums[i] >= 0) {
sumNums += nums[i]
} else {
break
}
}
//show string like this '-1-2-3-4-5-6-'
let nums = [1, 2, 3, 4, 5, 6];
let count = '-';
for(let i = 1; i < 10; i++) {
count += i + '-'
}
console.log(count);
//show 11 12 13 21 22 23 31 32 33 in console
for(let i = 1; i < 4; i++) {
for(let j = 1; j < 4 ; j++) {
console.log(i, j)
}
};
//make all numbers multiplied with itselves
let nums = [6, -54, 4, 33, -5];
for(let i = 0; i < nums.length; i++) {
console.log(Math.pow(nums[i], 2));
}
//fill an empty array with numbers from 1 to 10
const arrToPush = [];
for(let i = 1; i < 11; i++) {
arrToPush.push(i)
}
console.log(arrToPush);
//modify previous array to look like from 10 to 1
console.log(arrToPush.reverse());
//fill the object with info from one array and the object values from another array
let days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
let week = {};
let arr = [1, 2, 3, 4, 5, 6, 7];
for(let i = 0; i < 8; i++) {
week[days[i]] = arr[i];
}
console.log(week);
//fill the object with keys and values where values are between 10 and 20
let obj = {'a': 12, 'b': 21, 'c': 13, 'd': 23, 'e': 17};
let newObj = {};
for(let key in obj) {
if(obj[key] > 10 && obj[key] < 20) {
newObj[[key]] = obj[key]
}
}
console.log(newObj);
//fill and object with info from two arrays
let cloth = ['blouse', 'socks', 'shorts', 't-shirt', 'jacket'];
let colors = ['green', 'white', 'yellow', 'red', 'black'];
let order = {};
for(let i = 0; i < cloth.length; i++) {
order[cloth[i]] = colors[i]
}
console.log(order)
//multiple twice every value from the object
let obj = {'a': 1, 'b': 2, 'c': 3,
'd': 4, 'e': 5};
for(let key in obj) {
obj[key] = Math.pow(obj[key], 2)
}
console.log(obj);
//modify previous object swapping keys and values
let obj = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5};
let newObj = {};
for(let key in obj) {
newObj[obj[key]] = key;
}
console.log(newObj)
//increase values by 10%
let salaries = {
employee1: 100,
employee2: 200,
employee3: 300,
employee4: 400,
employee5: 500,
employee6: 600,
employee7: 700,
};
for(let key in salaries) {
if(salaries[key] >= 400) {
salaries[key] += salaries[key] * 0.1
}
}
console.log(salaries);
//fill the object from two arrays
let arr1 = [1, 2, 3, 4, 5];
let arr2 = [6, 7, 8, 9, 10];
let numObj = {};
for(let i = 0; i < arr1.length; i++) {
numObj[arr1[i]] = arr2[i]
}
console.log(numObj);
//sum all the keys and devote it with sum of all the values
let obj2 = {1: 6, 2: 7, 3: 8, 4:
9, 5: 10};
let sumKeys = 0;
let sumValues = 0;
for(let key in obj2) {
sumKeys += +key
sumValues += obj2[key]
}
let sumDevoteSum = sumKeys / sumValues
console.log(sumDevoteSum);
//fill the first array with the keys and the second with values of the object
let obj2 = {1: 6, 2: 7, 3: 8, 4:
9, 5: 10};
let arr3 = [];
let arr4 = [];
for(let key in obj2) {
arr3.push(+key);
arr4.push(obj2[key]);
}
console.log(arr3, arr4);
//fill the array with the object values that start with 1 and 2
let obj3 = {
1: 125,
2: 225,
3: 128,
4: 356,
5: 145,
6: 281,
7: 452,
};
let arr5 = [];
for(let key in obj3) {
if(obj3[key] < 300) {
arr5.push(obj3[key])
}
}
console.log(arr5);
// fill the object with numbers as keys and letters from array as values
let arr = ['a', 'b', 'c', 'd', 'e'];
let obj = {};
for(let i = 0; i < arr.length; i++) {
obj[i + 1] = arr[i];
}
console.log(obj);
//fill the objects with letters as keys and numbers as values
let arr = ['a', 'b', 'c', 'd', 'e'];
let obj = {};
for(let i = 0; i < arr.length; i++) {
obj[arr[i]] = i + 1
}
console.log(obj)
//calculate how many 3 and how many 2 are in the array
let arr = [1, 2, 3, 2, 4, 3, 5,
6, 3, 2, 3];
let threeSum = 0;
let twoSum = 0;
for (let i of arr) {
if(i === 3) {
threeSum++
}
if(i === 2) {
twoSum++
}
}
console.log(threeSum, twoSum);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment