Thursday, December 21, 2017

[JavaScript][Exercise] converts Date format

Write a function that converts user entered date formatted as M/D/YYYY to a format required by an API (YYYYMMDD). The parameter "userDate" and the return value are strings.

For example, it should convert user entered date "12/31/2014" to "20141231" suitable for the API.

Reference answer 1:

function formatDate(userDate) {
  var temp = userDate.split("/");
  if (temp[0].length < 2) temp[0] = '0' + temp[0];
  if (temp[1].length < 2) temp[1] = '0' + temp[1];
  return temp[2]+temp[0]+temp[1];
}

console.log(formatDate("12/31/2014"));

Reference answer 2:

function formatDate(userDate) {
    var d = new Date(userDate),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [year, month, day].join("");
}

console.log(formatDate("12/31/2014"));

Reference

https://stackoverflow.com/questions/23593052/format-javascript-date-to-yyyy-mm-dd

Friday, December 15, 2017

[JavaScript][Exercise] Throw Error if without argument.

Implement the ensure function so that it throws an error if called without arguments or the argument is undefined. Otherwise it should return the given value.

Reference answer

function ensure(value) {
  if(value === undefined) throw new Error('no arguments');
  return value;
}

Reference 

https://stackoverflow.com/questions/44874410/javascript-function-should-throw-an-error-if-called-without-arguments-or-an-argu

Wednesday, December 13, 2017

[Vue.js][Resolved] Interpolation inside attributes has been removed. Use v-bind or the colon shorthand instead

Error msg :
[Vue warn]: value="{{date}}": Interpolation inside attributes has been removed. Use v-bind or the colon shorthand instead. For example, instead of <div id="{{ val }}">, use <div :id="val">. 
Source Code:
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Untitle</title>
</head>
<body>
    <div id="app">
        <p>
            <label for="date">Date :</label>
            <input name="date" type="date" v-model="date" value="{{date}}"/>
        </p>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.4/vue.js"></script>
    <script>
    var data = {
        "date":"2017-01-03",
    };
    new Vue({
        el:'#app',
        data:data
    });
    </script>
</body>
</html>

What should do is already mentioned in the error message, change the red text to green text mentioned below:

Correction

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Untitle</title>
</head>
<body>
    <div id="app">
        <p>
            <label for="date">Date :</label>
            <input name="date" type="date" v-model="date" :value="date"/>
        </p>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.4/vue.js"></script>
    <script>
    var data = {
        "date":"2017-01-03",
    };
    new Vue({
        el:'#app',
        data:data
    });
    </script>
</body>
</html>

Reference

https://github.com/vuejs/vue/issues/2812

Monday, December 11, 2017

[Python3][Resolved] start_engine() takes 0 positional arguments but 1 was given

Error message

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-8f585077ddb5> in <module>()
     23 newCar = Car()
     24 print(newCar.color)
---> 25 print(newCar.start_engine())
     26 print(newCar.drive())
     27 print(newCar.stop_engine())

TypeError: start_engine() takes 0 positional arguments but 1 was given

Source code

class Car:
    color = "Blue"

    def start_engine():
        print("Starting the engine!")
    def drive():
        print("Driving the car!")
    def stop_engine():
        print("Turning off the car!")

newCar = Car()
print(newCar.color)
print(newCar.start_engine())
print(newCar.drive())
print(newCar.stop_engine())

Solution

Since the method in class is a class method but not a function, we need a 'self' parameter :
class Car:
    color = "Blue"

    def start_engine(self):
        print("Starting the engine!")
    def drive(self):
        print("Driving the car!")
    def stop_engine(self):
        print("Turning off the car!")

newCar = Car()
print(newCar.color)
print(newCar.start_engine())
print(newCar.drive())
print(newCar.stop_engine())

Reference

https://www.tutorialspoint.com/python/python_classes_objects.htm
https://stackoverflow.com/questions/18884782/typeerror-worker-takes-0-positional-arguments-but-1-was-given