“JS Window.Prompt” Kode Jawaban

prompt javascript

// window.prompt() => Prompt for user input and return the value:
const person = prompt("Please enter your name");

console.log( "Hello " + person + " !" );

// The 2nd argument (optional) holds the default value:
const color = prompt("Enter a color name", "red");
KostasX

JS Window.Alert

// window.alert(message);
/*
`window.alert` opens a dialog with an "Ok" button. Upon
receiving input, the dialog is closed.
*/

window.alert("Warning! Something has happened...");
/*
Note that this will pause code execution until
the dialog receives input. You can't fully get
around this, however using asynchronous
functions or promises, you can get other
statements to be called just after the dialog
is closed and before the dialog returns its
response.
*/
window.alert = (function() {
	const synchronous_confirm = window.alert;
	return async function(message) {
		return synchronous_confirm(message);
	};
})();
// OR
window.alert = (function() {
	const synchronous_confirm = window.alert;
	return function(message) {
		return new Promise((res, rej) => {
			try {
				res(synchronous_confirm(message));
			} catch (error) {
				rej(error);
			}
		});
	};
})();
MattDESTROYER

JS Window.Prompt

// window.prompt(message);
/*
`window.prompt` opens a confirmation dialog with an "Ok"
and "Cancel" button. Upon receiving input, the dialog is
closed and a boolean is returned. `window.prompt` returns
the input string if the "Ok" button was pressed or null
if "Cancel" was pressed.
*/

const user_input = window.prompt("Hello, what language do you code in?");

if (user_input && user_input.trim()) {
	window.alert(user_input + "! Cool!");
} else {
	window.alert("Oh no! You don't seem to have written anything...");
}
/*
Note that this will pause code execution until
the dialog receives input. You can't fully get
around this, however using asynchronous
functions or promises, you can get other
statements to be called just after the dialog
is closed and before the dialog returns its
response.
*/
window.prompt = (function() {
	const synchronous_confirm = window.prompt;
	return async function(message) {
		return synchronous_confirm(message);
	};
})();
// OR
window.prompt = (function() {
	const synchronous_confirm = window.prompt;
	return function(message) {
		return new Promise((res, rej) => {
			try {
				res(synchronous_confirm(message));
			} catch (error) {
				rej(error);
			}
		});
	};
})();
MattDESTROYER

Jawaban yang mirip dengan “JS Window.Prompt”

Pertanyaan yang mirip dengan “JS Window.Prompt”

Lebih banyak jawaban terkait untuk “JS Window.Prompt” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya