Challenges with printing in JavaScript tutorial on "Closures" at Code School

Just joined Stack Overflow and looking for help with a JavaScript closure problem from CodeSchool. Here's the issue:

The Dev Girls need to keep track of reported obstacle locations in order to create a list of danger zones for each obstacle when issuing warnings about remote-controlled laser beam sharks (they're in a rush). Your task is to modify the warning maker function to store new locations in an array called zones and display all current danger zones in every warning message for that specific obstacle.

Beware! There have been obstacle sightings in the Cove today!number obstacle(s) spotted at the location! This is Alert #count today for obstacle danger. Current danger zones are: zone1
zone2
zone3

I'm struggling to get the zones printed on separate lines. My code attempt so far:

function warningMaker( obstacle ){
  var count = 0;
  var zones = [];
  return function ( number, location ) {
    count++;
    zones.push(location + "/n");
    alert("Beware! There have been "+obstacle+" sightings in the Cove today!\n" +
          number+" "+obstacle+"(s) spotted at the "+location+"!\n" +
          "This is Alert #"+count+" today for "+obstacle+" danger.\n" +
          "Current danger zones are:\n" +
          zones);
  };
}

Any assistance would be greatly appreciated!

Answer №1

locations.push(area + "/n");

For a newline character, use a backslash instead of a forward slash. Try:

locations.push(area + "\n");

If you are passing a single argument to area, the previous fix should be sufficient. To display all locations in line, join them using Array.prototype.join() in the alert(...) section instead of just using locations.

locations.join('\n');

Answer №2

To accomplish this, you may utilize the `join` method available in the Array class.

alert("Warning! There have been "+obstacle+" incidents spotted in the Cove today!\n" +
      number+" "+obstacle+"(s) seen at the "+location+"!\n" +
      "This is Alert #"+count+" for "+obstacle+" danger today.\n" +
      "Current danger zones are:\n" +
      zones.join('\n'));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exchange of arrays between basic classes is made possible with ExtJS

I have two subclasses that inherit from a parent class. Class1 inherits from BaseClass Class2 inherits from BaseClass I create instances of both classes. When I display the array content of Class2, I see the contents of Class1 as well. http://jsfiddle ...

The placement of Bootstrap Datepicker is experiencing issues

I have integrated the Bootstrap Datepicker from Eternicode into my ASP.Net MVC website. While the functionality is working well, I am facing difficulty in positioning the datepicker modal using the orientation option mentioned in the documentation and code ...

Why is it possible to import the Vue.js source directly, but not the module itself?

The subsequent HTML code <!DOCTYPE html> <html lang="en"> <body> Greeting shown below: <div id="time"> {{greetings}} </div> <script src='bundle.js'></script& ...

An issue arises when attempting to remove or add attributes to the select box

In an attempt to change the 'disabled' and 'selected' properties, I am trying to set them to false and then apply the 'selected' and 'disabled' properties to the first option of a select box. Below is the code snippe ...

Loading two scripts in sequence, the first utilizing the "src" attribute while the second is an inline script

The HTML code below shows two script elements being added to the body of a webpage. <html> <body> <script> const first = document.createElement("script"); first.setAttribute("src", "./remote.js"); first.async = false; const second = doc ...

Is jQuery(document) loaded on 'focus'?

Whenever a user clicks on an input, I intend to activate a method. The code I currently have is: jQuery(document).on('focus click', function(e){ console.log("focused on " + $(e.target).attr('id')); }); However, when I focus on ...

Unlock the powers of Express, Passport, and Redis sessions!

Lately, I have been utilizing the default MemoryStore for my Express sessions and everything has been running smoothly. However, I encountered a setback where all session data was lost between restarts. To address this issue, I am now attempting to configu ...

Changing the size of an image in an HTML5 canvas

I have been attempting to generate a thumbnail image on the client side using Javascript and a canvas element. However, when I reduce the size of the image, it appears distorted. It seems as though the resizing is being done with 'Nearest Neighbor&apo ...

What is the best method to convert a standard PHP array into a JSON array while preserving the indexes?

Imagine starting with a basic array. <?php $a = array('a','b','c'); ?> Is there a way to create a JSON array as shown below? { '0':'a', '1':'b', '2':'c' } T ...

Should I use Mongoose schema methods or prototype functions?

I am curious about the functionality of functions and ES6 classes in relation to new objects created from a mongoose schema. I wonder if the functions are duplicated for each new object and what the best approach is when utilizing ES6 classes. To illustrat ...

Instructions for importing the 'child_process' module on the server side of a ReactJS application

Is there a proper way to utilize the 'child_process' module in a ReactJS application? ([email protected] / Linux) Various attempts have been made using different syntaxes without success: import { spawn } from 'child_process' ...

Image pop-ups that overlay text on the homepage

I'm facing an issue and I'm looking for a solution... Upon entering my homepage, I would like to display a popup image showcasing a new event so visitors can see it before accessing the website. I attempted to achieve this using JavaScript but w ...

The process of converting an array to lowercase

Hey everyone, I could really use your help with my Java program issue. I need to convert my array into a lowercase array. public class Main { public static char[] bubblesort(char[] plain) { int temp; for(int i=1; i<plain.length; i ...

Angular ng-repeat not populating the list properly, causing a collapse not to display

Currently, I am working on developing an app using Angular.js and Bootstrap UI, but I have run into a problem with a collapse navigation feature. The issue I am facing is that I have an ng-repeat that should be functioning properly. However, when I click ...

Using HTML5 chunks and web workers will not involve any uploading of files

I encountered an issue while working with html5 slice and webworker. It seems that when I try to upload a file using the uploadFile function, nothing is happening and the file is not being uploaded. <html> <head> <title>Uploa ...

MongoDB and JS code not processing the conditional statement

Here is the code snippet I am currently working with: if (checkUserExists(serverID, userID) === null) { console.log("was null"); addNewUserToDB(serverID, userID, username); } I am aiming for it to run only if mongoDB cannot find a match for both ...

Pinia fails to initialize in Vue.js router

I've been experimenting with Pinia for state management in my Vue application, but I've encountered an issue where Pinia is being used before it's initialized in the main.js file. Below is the code snippet from the routes file: import { cre ...

How to find the exact location using a relative index [^x] within an array, list, or dictionary in C#?

Ever since the release of C# version 8, developers have been introduced to a new method of indexing, as illustrated in this example: void Main() { Index idx = ^5; // counted from last to first element var sentence = new string[] { &quo ...

In JavaScript, the act of shuffling an array will produce consistent results

I have created a random array generator by shuffling an initial array multiple times. SHUFFLE FUNCTION const shuffle =(array) => { let currentIndex = array.length, temporaryValue, randomIndex; while (0 !== currentIndex) { randomIndex = Ma ...

Generating random values from an array in PHP without the need to refresh the page

Lately, I've been delving into the world of PHP for the first time in a project. However, I've encountered a problem that has me stumped – despite scouring various questions on Stack Overflow. My issue involves randomizing values from an array ...