JavaScript: Extend a 3-digit color code to a 6-digit color code

JavaScript fundamental (ES6 Syntax): Exercise-20 with Solution

Write a JavaScript program to extend a 3-digit color code to a 6-digit color code.

• Use Array.prototype.map(), String.prototype.split() and Array.prototype.join() to join the mapped array for converting a 3-digit RGB notated hexadecimal color-code to the 6-digit form.
• Array.prototype.slice() is used to remove # from string start since it's added once.

Sample Solution:

JavaScript Code:

``````//#Source https://bit.ly/2neWfJ2
const extend_Hex = shortHex =>
'#' +
shortHex
.slice(shortHex.startsWith('#') ? 1 : 0)
.split('')
.map(x => x + x)
.join('');

console.log(extend_Hex('#03f'));
console.log(extend_Hex('05a'));
```
```

Sample Output:

```#0033ff
#0055aa
```

Pictorial Presentation:

Flowchart:

Live Demo:

See the Pen javascript-basic-exercise-1-20 by w3resource (@w3resource) on CodePen.

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

JavaScript: Tips of the Day

Returns an array of n-tuples of consecutive elements

Example:

```const tips_arr = (n, arr) =>  n > arr.length ? [] : arr.slice(n - 1).map((v, i) =>[...arr.slice(i, i + n - 1), v]);
console.log(tips_arr(2, [1, 2, 3, 4, 5]));
console.log(tips_arr(3, [1, 2, 3, 4, 5]));
console.log(tips_arr(5, [1, 2, 3, 4]));
```

Output:

```[[1, 2], [2, 3], [3, 4], [4, 5]]
[[1, 2, 3], [2, 3, 4], [3, 4, 5]]
[]
```