-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ex.1.html
58 lines (51 loc) · 2.11 KB
/
Ex.1.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css?family=Fira+Sans+Extra+Condensed" rel="stylesheet">
<title>Document</title>
</head>
<body>
<img src="https://www.codewars.com/users/stefan835/badges/large" alt="codewars-badge">
<h1>Ex.1</h1>
<h3><a href="Ex.2.html">Link to next!</a></h3>
<p>Write a function that accepts an array of 10 integers (between 0 and 9), that returns a string of those numbers in
the form of a phone number.</p>
<h4>Example:</h4>
<i>createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) // => returns "(123) 456-7890"</i>
<p>The returned format must be correct in order to complete this challenge.
Don't forget the space after the closing parenthesis! </p>
<h2>My solution</h2>
<div class="code">
<pre>function createPhoneNumber(numbers) {<br/> const result = `(${numbers.slice(0, 3)}) ${numbers.slice(3, 6)}-${numbers.slice(6)}`;<br/> return result.replace(/,/g, '')<br/>}</pre>
</div>
<h2>Best solution</h2>
<div class="code">
<pre>function createPhoneNumber(numbers){<br/> var format = "(xxx) xxx-xxxx";<br/><br/> for(var i = 0; i < numbers.length; i++)<br/> {<br/> format = format.replace('x', numbers[i]);<br/> }<br/> return format;<br/>}</pre>
</div>
<h2 class="passed">PASSED</h2>
<h3>Time: 315ms</h3>
<script>
//MY SOLUTION
function createPhoneNumber(numbers) {
const result = `(${numbers.slice(0, 3)}) ${numbers.slice(3, 6)}-${numbers.slice(6)}`;
return result.replace(/,/g, '')
}
console.log(createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]))
// BEST SOLUTION
// function createPhoneNumber(numbers){
// var format = "(xxx) xxx-xxxx";
//
// for(var i = 0; i < numbers.length; i++)
// {
// format = format.replace('x', numbers[i]);
// }
// return format;
// }
</script>
</body>
</html>