educative.io

Question: func MySqrt

The following question:

What is/are the return value(s) of the following function MySqrt for f equals to -2 ?

func MySqrt(f float64)(float64, error) {    //return an error as second parameter if invalid input    if (f < 0) {        return float64(math.NaN()), errors.New("I won't be able to do a sqrt of negative number!")    }    //otherwise use default square root function    return math.Sqrt(f), nil}

has a correct answer of Both B and C; However, when running the same exact code snippet:

package main

import (
	"fmt"
	"math"
	"errors"
)

func MySqrt(f float64)(float64, error) {
    //return an error as second parameter if invalid input
    if (f < 0) {
        return float64(math.NaN()), errors.New("I won't be able to do a sqrt of negative number!")
    }
    //otherwise use default square root function
    return math.Sqrt(f), nil
}

func main() {
	fmt.Println(MySqrt(-2))
}

it prints

NaN I won’t be able to do a sqrt of negative number!

Is there’s something I’m not getting ?

Hi @Yusuf,

This is Maham Amjad from Educative. I noticed your feedback here, and I’m glad you reached out to us.

Running the code snippet will give option A as a correct answer. Yes, you are correct!
Your answer is not wrong. The issue is on our side, and we are grateful that you reported it. I have updated the quiz to fix this mistake. Apologies for the inconvenience.

Thank you again for pointing this out! Please feel free to reach out if you have any other queries or concerns.

Best Regards,
Maham Amjad| Developer Advocate
Educative Inc.

3 Likes