You are on page 1of 4

// Array’s function

// 1)concat

function concat(){
var array1 = [1, 2, 3];
var array2 = [4, 5, 6];
for (var i = 0; i < array2.length; i++) {
array1[array1.length] = array2[i];
}
console.log(array1);
}
concat()

// 2)IndexOf

function indexof(array, value) {


for (i = 0; i < array.length; i++) {
if (array[i] === value) {
return i;
}
}
return ("-1");
}

console.log(indexof([1, 2, 3, 4], 3));

// 3)Join

let arrayjoin = ["h","e","l","l","o"];


let join = "";

for(i=0; i<arrayjoin.length; i++){


joinValue += arrayjoin[i];
if (i !== arrayjoin.length - 1){
joinValue +="-"
}
}
console.log(joinValue)

// 4)lastIndexof
function indexof(array, value) {
for (i = array.length - 1; i >= 0; i--) {
if (array[i] === value) {
return i;
}
}
return -1;
}

console.log(indexof([1, 2, 3, 4], 5)); //-1

// 5)arrPush

function arrPush(arraypush, value) {


arr[arraypush.length1] = value;
return arraypush.length1;
}

let arraypush = [1, 2, 3, 4];


let length1 = arrPush(arraypush, 4);

// 6)arrPop

function arrPop() {
let last = array[array.length - 1];
array.length = array.length - 1;
return last;
}
let last = arrPop(array);
console.log(array);

// 7)arrShift(arr)

function arrShift(array) {
let value = array[0];
for (i = 0; i < array.length - 1; i++) {
array[i] = array[i + 1];
}
array.length = array.length - 1;
return value;
}
let value = arrShift(array);
console.log(array);

// 8)arrUnshift

function arrUnshift(array, value) {


for (let i = array.length - 1; i >= 0; i--) {
array[i + 1] = array[i];
}
array[0] = value;
return array.length;
}
let array = [1, 2, 3, 4];
let length = arrUnshift(array, 0);
console.log(array);

// 9)arrSort

function sort(array) {
for (let i = 0; i < array.length; i++) {
for (let j = i + 1; j < array.length; j++) {
if (array[i] > array[j]) {

let temp = array[i];

array[i] = array[j];

array[j] = temp;
}
}
}
return array;
}

// ---------------------------------------------------------------

// ---------------------------------------------------------------

// ---------------------------------------------------------------

// JavaScript Assignment 2 Functions arrays inbuilt


// ---------------------------------------------------------------

let ex1 = [1, 2, 3];


let arr2 = ['a', 'b', 'c'];
let arr3 = ex1.concat(arr2);
console.log(arr3);
//---------------------------------------------------------------
let ex2 = [1, 2, 3, 4, 5];
let index = ex2.indexOf(3);
console.log(index); // 2 - 3 is @ 2nd index
//---------------------------------------------------------------
let ex3 = [1, 2, 3, 4, 5];
let str = ex3.join("*");
console.log(str); // joins * inbetween each
//---------------------------------------------------------------
let ex4 = [1, 2, 3, 4, 5];
let index1 = ex4.indexOf(1);
let index2 = ex4.lastIndexOf(2);
console.log(index1);
console.log(index2);
// does not start counting from the back. It returns the
//index starting from the beginning of the last matching item.
//---------------------------------------------------------------
let ex5 = [1, 2, 3];
ex5.push(4);
console.log(ex5);
//---------------------------------------------------------------
let ex6 = [1, 2, 3];
ex6.pop(2);
console.log(ex6); //removes last one
//---------------------------------------------------------------
let ex7 = [1, 2, 3];
ex7.shift();
console.log(ex7); // first one will be removed
//---------------------------------------------------------------
let ex8 = [3, 4, 5];
ex8.unshift(2);
console.log(ex8);//returns with adding value given
//---------------------------------------------------------------
let ex9 = [3, 2, 1];
ex9.sort();
console.log(ex9); // sorts the array
//---------------------------------------------------------------

You might also like