If I studied JavaScript for a week and I can't even solve the string output problem, should I close it

Asked 1 years ago, Updated 1 years ago, 436 views

I'm going to do a coding test like this..

When given => ?
Enter => ?
Return => return
Output => console.log()

It came out like this, but I don't think I understood the problem method yet.
Please give me some tips

javascript

2023-05-05 17:47

1 Answers

I think it means to come up with a function that meets the given requirements.

Return => return

The only thing that can be returned is the function. So, for example, the problem is

If you give a number, output whether the number is negative, positive, or zero, and return the form of the absolute value of the number. Do not use the Math module.

The answer to be submitted is:

// And you can name any function as long as the problem does not specify it
function abs(num) {
    if (num == 0) {
        console.log ('Exactly zero');
    } } else if (num > 0) {
        console.log ('positive');
    } } else {
        console.log ('negative');
        Since the absolute value of num = num * -1; // 0 is 0, and the absolute value of positive is positive itself, you can multiply -1 only when it is negative and make it positive
    }
    return num;
}

That's how it works.

You posted a question about the question, but I don't know if I understood the question well. I hope your answer to this question has been answered.


2023-05-05 18:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.