JavaScript-Developer-I Exam Question 166

A developer is creating a simple webpage with a button. When a user clicks this button for the first time, a message is displayed.
The developer wrote the JavaScript code below, but something is missing. The message gets displayed every time a user clicks the button, instead of just the first time.
01 function listen(event) {
02 alert ( 'Hey! I am John Doe') ;
03 button.addEventListener ('click', listen);
Which two code lines make this code work as required?
Choose 2 answers
  • JavaScript-Developer-I Exam Question 167

    A developer wants to use a try...catch statement to catch any error that countSheep () may throw and pass it to a handleError () function.
    What is the correct implementation of the try...catch?
  • JavaScript-Developer-I Exam Question 168

    A class was written to represent items for purchase in an online store, and a secondclass Representing items that are on sale at a discounted price. THe constructor sets the name to the first value passed in. The pseudocode is below:
    Class Item {
    constructor(name, price) {
    ... // Constructor Implementation
    }
    }
    Class SaleItem extends Item {
    constructor (name, price, discount) {
    ...//Constructor Implementation
    }
    }
    There is a newrequirement for a developer to implement a description method that will return a brief description for Item and SaleItem.
    Let regItem =new Item('Scarf', 55);
    Let saleItem = new SaleItem('Shirt' 80, -1);
    Item.prototype.description = function () { return 'This is a ' + this.name; console.log(regItem.description()); console.log(saleItem.description()); SaleItem.prototype.description = function () { return 'This is a discounted ' + this.name; } console.log(regItem.description()); console.log(saleItem.description()); What is the output when executing the code above ?
  • JavaScript-Developer-I Exam Question 169

    Refer to the following code:
    class Vehicle{
    constructor(plate){
    this.plate = plate;
    }
    }
    class Truck extends Vehicle{
    constructor(plate, weight){
    //Missing code
    this.weight = weight;
    }
    displayWeight(){
    console.log(`The truck ${this.plate} has a weight of ${this.weight}lb.`);
    }
    }let myTruck = new Truck('123Ab',5000);
    myTruck.displayWeight();
    Which statement should be added to missing code for the code to display 'The truck 123AB has a weight of 5000lb.
  • JavaScript-Developer-I Exam Question 170

    Refer to the code below:
    Const searchTest = 'Yay! Salesforce is amazing!" ;
    Let result1 = searchText.search(/sales/i);
    Let result 21 = searchText.search(/sales/i);
    console.log(result1);
    console.log(result2);
    After running this code, which result is displayed on the console?