Salesforce Certified JavaScript Developer (JS-Dev-101) Questions and Answers
A developer is setting up a new Node.js server with a client library that is built using events and callbacks.
The library:
Will establish a web socket connection and handle receipt of messages to the server.
Will be imported with require, and made available with a variable called ws.
The developer also wants to add error logging if a connection fails.
Given this information, which code segment shows the correct way to set up a client with two events that listen at execution time?
A team at Universal Containers works on a big project and uses yarn to manage the project ' s dependencies.
A developer added a dependency to manipulate dates and pushed the updates to the remote repository. The rest of the team complains that the dependency does not get downloaded when they execute yarn .
What could be the reason for this?
Code:
01 let array = [1, 2, 3, 4, 4, 5, 4, 4];
02 for (let i = 0; i < array.length; i++) {
03 if (array[i] === 4) {
04 array.splice(i, 1);
05 i--;
06 }
07 }
What is the value of array after execution?
A developer removes the HTML class attribute from the checkout button, so now it is simply:
< button > Checkout < /button >
There is a test to verify the existence of the checkout button, however it looks for a button with class= " blue " . The test fails because no such button is found.
Which type of test category describes this test?
A developer wrote the following code:
01 let x = object.value;
02
03 try {
04 handleObjectValue(x);
05 } catch(error) {
06 handleError(error);
07 }
The developer has a getNextValue function to execute after handleObjectValue(), but does not want to execute getNextValue() if an error occurs. How can the developer change the code to ensure this behavior?
Refer to the code:
01 const exec = (item, delay) = >
02 new Promise(resolve = > setTimeout(() = > resolve(item), delay));
03
04 async function runParallel() {
05 const [result1, result2, result3] = await Promise.all(
06 [exec( ' x ' , ' 100 ' ), exec( ' y ' , ' 500 ' ), exec( ' z ' , ' 100 ' )]
07 );
08 return `parallel is done: ${result1}${result2}${result3}`;
09 }
Which two statements correctly execute runParallel()?
Given the code below:
01 function Person(name, email) {
02 this.name = name;
03 this.email = email;
04 }
05
06 const john = new Person( ' John ' , ' john@email.com ' );
07 const jane = new Person( ' Jane ' , ' jane@email.com ' );
08 const emily = new Person( ' Emily ' , ' emily@email.com ' );
09
10 let usersList = [john, jane, emily];
Which method can be used to provide a visual representation of the list of users and to allow sorting by the name or email attribute?
static delay = async delay = > {
return new Promise(resolve = > {
setTimeout(resolve, delay);
});
};
static asyncCall = async () = > {
await delay(1000);
console.log(1);
};
console.log(2);
asyncCall();
console.log(3);
Assume delay and asyncCall are in scope as functions.
What is logged to the console?
A developer wants to create a simple image upload using the File API.
HTML:
< input type= " file " onchange= " previewFile() " >
< img src= " " height= " 200 " alt= " Image preview... " / >
JavaScript:
01 function previewFile() {
02 const preview = document.querySelector( ' img ' );
03 const file = document.querySelector( ' input[type=file] ' ).files[0];
04 // line 4 code
05 reader.addEventListener( " load " , () = > {
06 preview.src = reader.result;
07 }, false);
08 // line 8 code
09 }
Which code in lines 04 and 08 allows the selected local image to be displayed?
Refer to the code snippet:
01 function getAvailableilityMessage(item) {
02 if (getAvailableility(item)) {
03 var msg = " Username available " ;
04 }
05 return msg;
06 }
What is the return value of msg when getAvailableilityMessage( " newUserName " ) is executed and getAvailableility( " newUserName " ) returns false?
A developer creates a simple webpage with an input field. When a user enters text and clicks the button, the actual value must be displayed in the console:
HTML:
< input type= " text " value= " Hello " name= " input " >
< button type= " button " > Display < /button >
JavaScript:
01 const button = document.querySelector( ' button ' );
02 button.addEventListener( ' click ' , () = > {
03 const input = document.querySelector( ' input ' );
04 console.log(input.getAttribute( ' value ' ));
05 });
When the user clicks the button, the output is always " Hello " .
What needs to be done to make this code work as expected?
Given the code:
01 function GameConsole(name) {
02 this.name = name;
03 }
04
05 GameConsole.prototype.load = function(gamename) {
06 console.log( ' ${this.name} is loading a game: ${gamename}.... ' );
07 }
08
09 function Console16bit(name) {
10 GameConsole.call(this, name);
11 }
12
13 Console16bit.prototype = Object.create(GameConsole.prototype);
14
15 // insert code here
16 console.log( ' ${this.name} is loading a cartridge game: ${gamename}.... ' );
17 }
18
19 const console16bit = new Console16bit( ' SNEGeneziz ' );
20 console16bit.load( ' Super Monic 3x Force ' );
What should a developer insert at line 15?
A developer writes the code below to return a message to a user attempting to register a new username. If the username is available, a variable named msg is declared and assigned a value on line 03.
function getAvailabilityMessage(item) {
if (getAvailability(item)) {
var msg = " Username available " ;
return msg;
}
}
01 function Animal(size, type) {
02 this.type = type || ' Animal ' ;
03 this.canTalk = false;
04 }
05
06 Animal.prototype.speak = function() {
07 if (this.canTalk) {
08 console.log( " It spoke! " );
09 }
10 };
11
12 let Pet = function(size, type, name, owner) {
13 Animal.call(this, size, type);
14 this.size = size;
15 this.name = name;
16 this.owner = owner;
17 }
18
19 Pet.prototype = Object.create(Animal.prototype);
20 let pet1 = new Pet();
Given the code above, which three properties are set for pet1?
Given two expressions, exp1 and exp2, which two valid ways return the logical AND of the two expressions and ensure it is a Boolean?
Which statement allows a developer to update the browser navigation history without a page refresh?
Refer to the following array:
let arr = [1, 2, 3, 4, 5];
Which two lines of code result in a second array, arr2, created such that arr2 is a reference to arr?
A Node.js server library uses events and callbacks. The developer wants to log any issues the server has at boot time.
Which code logs an error with an event?
Given:
const str = ' Salesforce ' ;
Which two statements result in ' Sales ' ?
Refer to the following code:
01 class Ship {
02 constructor(size) {
03 this.size = size;
04 }
05 }
06
07 class FishingBoat extends Ship {
08 constructor(size, capacity){
09 //Missing code
10 this.capacity = capacity;
11 }
12 displayCapacity() {
13 console.log( ' The boat has a capacity of ${this.capacity} people. ' );
14 }
15 }
16
17 let myBoat = new FishingBoat( ' medium ' , 10);
18 myBoat.displayCapacity();
Which statement should be added to line 09 for the code to display
The boat has a capacity of 10 people?
Refer to the code below:
01 const server = require( ' server ' );
02
03 // Insert code here
A developer imports a library that creates a web server. The imported library uses events and callbacks to start the server.
Which code should be inserted at line 03 to set up an event and start the web server?
Console logging methods that allow string substitution:
Given two expressions var1 and var2, what are two valid ways to return the concatenation of the two expressions and ensure it is data type string?
Refer to the code below:
01 function myFunction(reassign) {
02 let x = 1;
03 var y = 1;
04
05 if (reassign) {
06 let x = 2;
07 var y = 2;
08 console.log(x);
09 console.log(y);
10 }
11
12 console.log(x);
13 console.log(y);
14 }
What is displayed when myFunction(true) is called?
A developer uses a parsed JSON string to work with user information as in the block below:
01 const userInformation = {
02 " id " : " user-01 " ,
03 " email " : " user01@universalcontainers.demo " ,
04 " age " : 25
05 };
Which two options access the email attribute in the object?
A developer wants to use a module named universalContainerslib and then call functions from it. How should a developer import every function from the module and then call the functions foo and bar?
A test searches for:
< button class= " blue " > Checkout < /button >
But the actual HTML is:
< button > Checkout < /button >
The test fails because it expects a class that no longer exists.
What type of test outcome is this?
Refer to the code:
01 const event = new CustomEvent(
02 // Missing code
03 );
04 obj.dispatchEvent(event);
A developer needs to dispatch a custom event called update to send information about recordId.
Which two options can be inserted at line 02?
A developer wants to use a module called DatePrettyPrint.
This module exports one default function called printDate().
How can the developer import and use printDate()?
A developer initiates a server with the file server.js and adds dependencies in the source code ' s package.json that are required to run the server.
Which command should the developer run to start the server locally?
Refer to the code below (corrected to use a template literal on line 08):
01 let car1 = new Promise((_, reject) = >
02 setTimeout(reject, 2000, " Car 1 crashed in " )
03 );
04 let car2 = new Promise(resolve = >
05 setTimeout(resolve, 1500, " Car 2 completed " )
06 );
07 let car3 = new Promise(resolve = >
08 setTimeout(resolve, 3000, " Car 3 completed " )
09 );
10
11 Promise.race([car1, car2, car3])
12 .then(value = > {
13 let result = `${value} the race.`;
14 })
15 .catch(err = > {
16 console.log( " Race is cancelled. " , err);
17 });
What is the value of result when Promise.race executes?
A developer creates a new web server that uses Node.js. It imports a server library that uses events and callbacks for handling server functionality. The server library is imported with require and is made available to the code by a variable named server. The developer wants to log any issues that the server has while booting up.
Which code logs an error at boot time with an event?
Given the JavaScript below:
function onLoad() {
console.log( " Page has loaded! " );
}
Where can the developer see the log statement after loading the page in the browser?
Refer to the code:
const pi = 3.1415926;
What is the data type of pi?
Refer to the code below:
01 async function functionUnderTest(isOK) {
02 if (isOK) return ' OK ' ;
03 throw new Error( ' not OK ' );
04 }
Which assertion accurately tests the above code?
A team at Universal Containers works on a big project and uses Yarn to deal with the project’s dependencies. A developer added a dependency to manipulate dates and pushed the updates to the remote repository. The rest of the team complains that the dependency does not get downloaded when they execute yarn.
What could be the reason for this?
Refer to the code below (assuming Promise.race is intended):
let cat3 = new Promise(resolve = >
setTimeout(resolve, 3000, " Cat 3 completes " )
);
Promise.race([cat1, cat2, cat3])
.then(value = > {
let result = `${value} the race.`;
})
.catch(err = > {
console.log( " Race is cancelled: " , err);
});
(Assuming cat1 and cat2 are similar to earlier examples: cat2 resolves fastest.)
What is the value of result when Promise.race executes?
Refer to the code below:
const pi = 3.1415926;
What is the data type of pi?
Universal Containers (UC) just launched a new landing page, but users complain that the website is slow. A developer found some functions that might cause this problem. To verify this, the developer decides to execute everything and log the time each of these three suspicious functions consumes.
01 console.time( ' Performance ' );
02
03 maybeAHeavyFunction();
04
05 thisCouldTakeTooLong();
06
07 orMaybeThisOne();
08
09 console.endTime( ' Performance ' );
Which function can the developer use to obtain the time spent by every one of the three functions?
Which three browser specific APIs are available for developers to persist data between page loads?
Refer to the code below:
< html >
< body >
< div id= " logo " > Hello Logo! < /div >
< button id= " test " > Click me < /button >
< /body >
< script >
function printMessage(event) {
console.log( ' This is a test message ' );
}
let el = document.getElementById( ' test ' );
el.addEventListener( " click " , printMessage, false);
< /script >
< /html >
Which action should be done?
Refer to the code below:
01 let first = ' Who ' ;
02 let second = ' What ' ;
03 try {
04 try {
05 throw new Error( ' Sad trombone ' );
06 } catch (err) {
07 first = ' Why ' ;
08 throw err;
09 } finally {
10 second = ' When ' ;
11 }
12 } catch (err) {
13 second = ' Where ' ;
14 }
What are the values for first and second once the code executes?
Original code:
01 let requestPromise = client.getRequest;
03 requestPromise().then((response) = > {
04 handleResponse(response);
05 });
The developer wants to gracefully handle errors from a Promise-based GET request.
Which code modification is correct?
01 function changeValue(obj) {
02 obj.value = obj.value / 2;
03 }
04 const objA = {value: 10};
05 const objB = objA;
06
07 changeValue(objB);
08 const result = objA.value;
What is the value of result after the code executes?