Table of contents
Introduction
Namaste, In this blog I will discuss 10 code scenarios on Javascript .
Identify the output of the below program
let arr = [,,"ram",10]; let arr2 = arr.toSorted(); console.log(arr2);
a.error
b.[undefined, undefined,10,'ram']
c.[10, 'ram', undefined, undefined]
d.[10,'ram']
e.[10,'ram', null, null]
Answer:c
Reason:Empty values are converted to undefined.
Identify the output of the below program
let arr = [100,21,13]; let arr2 = arr.toSorted(); console.log(arr2);
a.[100,21,13]
b.[100,13,21]
c.[13,21,100]
d.[21,13,100]
e.error
Answer:b
Reason:The elements are converted to string then sorted based on ASCII values.
Identify the output of the below program
let arr = [100,21,13]; let arr2 = arr.toReversed(); console.log(arr2);
a.[100,21,13]
b.[13,100,21]
c.[13,21,100]
d.[100,13,21]
e.error
Answer:c
Reason:A new reversed array is created.
Identify the output of the below program
let arr = [100,21,13]; let arr2 = arr.with(2,999); console.log(arr2);
a.999
b.13
c.[100,21,999]
d.error
e.[100,13,999]
Answer:c
Reason:Returns a changed updated array
Identify the output of the below program
let arr = [11,29,63,90,29,74]; let v = arr.findLast(arg => arg > 20); console.log(v);
a.29
b.63
c.90
d.74
e.error
Answer:d
Reason:The last value greater than 20 is 74
Identify the output of the below program
let nums = [9,8,5,2,4,1,3,4,5]; let a = nums.findLast(arg => { if (arg==5 || arg == 8) return arg; }); console.log(a);
a.5
b.8
c.9
d.2
e.error
Answer:a
Reason:The last matching value is 5.
Identify the output of the below program
let arr = [100,21,13]; let arr2 = arr.toReversed(); console.log(arr);
a.[100,21,13]
b.[13,21,100]
c.[21,13,100]
d.[100,13,21]
e.error
Answer: a
Reason: The host array is not reversed.
Identify the output of the below program
let arr = [11,29,63,90,29,74]; let v = arr.findLast(arg => arg < 34); console.log("last value", v);
a.11
b.29
c.63
d.error
e.no output
Answer:b
Reason:The last value less than 34 is 29.
Identify the output of the below program
let arr = [1, 2, 3]; let arr2 = arr.with(0, 6); console.log(arr2);
a.6
b.[0,2,3]
c.[1,2,6]
d.[6,2,3]
e.error
Answer:d
Reason:The with() function returns the updated array.
Identify the output of the below program
let users = [ { id: 1, name: 'Blic' }, { id: 2, name: 'Balram' }, { id: 3, name: 'Baldev' }, ]; let lastUser = users.findLast((user) => user.name.startsWith('B')); console.log(lastUser.name);
a.Blic
b.Balram
c.Baldev
d.error
e.undefined
Answer:c
Reason:The last matching value is Baldev