Skip to main content

Command Palette

Search for a command to run...

[JavaScript] Developer Skills Coding Challenge

Updated
•View as Markdown
[JavaScript] Developer Skills Coding Challenge

Coding Challenge #1

Given an array of forecasted maximum temperatures, the thermometer displays a string with the given temperatures. Example: [17, 21, 23] will print "... 17ºC in 1 days ... 21ºC in 2 days ... 23ºC in 3 days ..." Your tasks:

  1. Create a function 'printForecast' which takes in an array 'arr' and logs a string like the above to the console. Try it with both test datasets.

  2. Use the problem-solving framework: Understand the problem and break it up into sub-problems!

Test data: § Data 1: [17, 21, 23] § Data 2: [12, 5, -5, 0, 4]


1) Understand the problem

  • Array transformed to string, separated by ... -What is the value of 'x(number) days' in the string? answer: index + 1

2) Breaking up into sub-problems

  • Transform the array into a string
  • Transform each element to a string with ºC
  • Strings need to contain day
  • Add ... between elements and start and end of the string
  • Log string to console

  • My first answer

const temp1 = [17, 21, 23];
const temp2 = [12, 5, -5, 0, 4];

const printForecast = function (arr) {
    for (let i = 0; i < arr.length; i++) {
    const forecast = `... ${arr[i]}ºC in ${i + 1} days...`;
    console.log(forecast);
  }
};

printForecast(temp1);
printForecast(temp2);

Result:

Capture.PNG

This result does not fit into the expectation of the function result, not in the 1 linear line result.

  • Final answer
const temp1 = [17, 21, 23];
const temp2 = [12, 5, -5, 0, 4];

const printForecast = function (arr) {
  let str = '';
  for (let i = 0; i < arr.length; i++) {
    str += `${arr[i]}ºC in ${i + 1} days... `;
  }
  console.log('... ' + str);
};

printForecast(temp1);
printForecast(temp2);

Result:

Capture2.PNG

Perfect fit!!

More from this blog

Jayden's Blog

166 posts